0
这是撕裂我的头发,更改的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">
在后面的IM我的代码中动态地将项目添加到列表
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);
}
}
但是,背景颜色不会改变,列表不会更新。
任何想法为什么? 非常感谢
据我所知,您不能在ItemsSource模式下将项目添加到列表中,您需要更改项目源。 –
我的xaml中的ListView的类型是System.Windows.Controls.ListView,这个绑定是不正确的? –
如果你想改变你的列表视图的内容,不管DataContext的'Item'属性是什么,或者不绑定这些itemsource,然后你就可以直接改变列表视图项目。 –