Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改“填充”上ListViewItem的背景色_C#_Wpf_Listview_Listviewitem - Fatal编程技术网

C# 更改“填充”上ListViewItem的背景色

C# 更改“填充”上ListViewItem的背景色,c#,wpf,listview,listviewitem,C#,Wpf,Listview,Listviewitem,这是在扯我的头发 我有一个列表视图 <ListView Canvas.Left="1045" Canvas.Top="667" FontSize="25" ItemsSource="{Binding Items}" FontFamily="Gill Sans MT" Height="173" Name="lvContact" Width="536" SelectionChanged="lvContact_SelectionChanged"> 在我的代码隐藏中,我动态地

这是在扯我的头发

我有一个列表视图

<ListView Canvas.Left="1045"  Canvas.Top="667"  FontSize="25" ItemsSource="{Binding Items}"   FontFamily="Gill Sans MT" Height="173" Name="lvContact" Width="536" SelectionChanged="lvContact_SelectionChanged">

在我的代码隐藏中,我动态地向列表中添加一个项目

public void UpdateContactList(Hashtable contactList)
{
    this.lvContact.Items.Clear();

    SortedDictionary<string,string> sortedContactList = new SortedDictionary<string,string>();


    foreach (DictionaryEntry de in contactList)
    {
        sortedContactList.Add(de.Key.ToString(), de.Value.ToString());
    }


    foreach (var de in sortedContactList)
    {
        System.Windows.Controls.ListViewItem contactItem = new System.Windows.Controls.ListViewItem();
        string contactItemString = de.Key.ToString();

        System.Windows.Controls.ListViewItem text = new System.Windows.Controls.ListViewItem();

        text.Content = contactItemString;
        if (de.Value == "NLN")
        {
            text.Background = Brushes.Green;
        }
        else
        {
            text.Background = Brushes.Gray;
        }
        lvContact.Items.Add(text);
    }
}
public void UpdateContactList(哈希表联系人列表)
{
this.lvContact.Items.Clear();
SortedDictionary sortedContactList=新的SortedDictionary();
foreach(字典进入联系人列表)
{
添加(de.Key.ToString(),de.Value.ToString());
}
foreach(分拣联系人列表中的var de)
{
System.Windows.Controls.ListViewItem contactItem=新建System.Windows.Controls.ListViewItem();
string contactItemString=de.Key.ToString();
System.Windows.Controls.ListViewItem text=新建System.Windows.Controls.ListViewItem();
text.Content=contactItemString;
如果(de.Value==“NLN”)
{
text.Background=画笔.Green;
}
其他的
{
text.Background=画笔.Gray;
}
lvContact.Items.Add(文本);
}
}
但是,背景颜色永远不会改变,列表也不会更新

你知道为什么吗?
非常感谢

列表视图可以绑定到
项目资源
,也可以手动指定
列表视图。项目
。你不能两者兼得

ListView定义绑定了
ListView.ItemsSource
,因此无法手动指定
ListView.Items

由于您的
ItemsSource
绑定到一个属性
Items
,因此我假设您有一个
列表
可观察集合
的地方,名为
Items
,其中包含ListView中的项目。要修改ListView的项,您应该修改此集合

要根据值更改背景颜色,我将使用DataTrigger。这将允许您保持ItemsSource绑定,并将数据与UI分离

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="Gray" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Value}" Value="NLN">
            <Setter Property="Background" Value="Green" />
        </DataTriggers>
    </Style.Triggers>
</Style>


据我所知,您无法在ItemsSource模式下向列表添加项目,您需要更改项目源。我的xaml中的ListView类型为System.Windows.Controls.ListView是否绑定不正确?如果要更改ListView的内容,请更改DataContext的“item”属性包含的内容。。。或者不绑定itemsource,那么您就可以直接更改列表视图项。