更改绑定的LongListSelector或列表框的项目颜色

问题描述:

我一直在尝试更改ListBox中的TextBlock的颜色,它从绑定中获取它的颜色。更改绑定的LongListSelector或列表框的项目颜色

<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Foreground="{Binding ItemColor, Converter={StaticResource ColorConverter}}" Style="{StaticResource posttitle}" d:LayoutOverrides="Width"/> 

下面是其中初始期间工作的转换器渲染:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
    if (value == null) 
     return new SolidColorBrush(Colors.Red); 

    Color colorValue = (Color)value; 

    return new SolidColorBrush(colorValue); 
} 

SelectionChanged事件期间,我分配了一个新的颜色像这样的项目:

private void List_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var listbox = (LongListSelector)sender; 

    if (listbox.SelectedItem == null) 
     return; 

    MyItem item = (MyItem)listbox.SelectedItem; 

    if (item.Clicked) 
    { 
     // Change some value 
     item.Clicked = true; 
     item.ItemColor = new Color() { A = 0xFF, R = 0xBD, G = 0xB7, B = 0x6B }; 
    } 
} 

如果我把一个断点并检查datacontext,我可以看到源中的值已更改,但在视觉上LongListSelector不会改变外观。绑定是通过ObservableCollectionItemColor通知更改。

任何帮助表示赞赏。

+0

显然'ItemColor'没有引发'PropertyChanged'事件。发布“MyItem”类的源代码。 –

您没有提供足够的信息,但根据您提供的代码,它看起来像设置为item.ItemColor时,ItemColor的PropertyChanged事件未被触发。

MyItem应执行INotifyPropertyChanged并致电PropertyChanged(this, new PropertyChangedEventArgs("ItemColor"))