更改绑定的 LongListSelector 或列表框的项目颜色
我一直在尝试更改从绑定获取颜色的 ListBox
中的 TextBlock
的颜色。
<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 };
}
}
如果我放置一个断点并检查数据上下文,我可以看到源中的值已更改,但视觉上 < code>LongListSelector 没有改变外观。绑定到 ObservableCollection
,并且 ItemColor
确实通知更改。
任何帮助表示赞赏。
I have been trying to change the color of a TextBlock
in a ListBox
that gets it's color from a binding.
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Foreground="{Binding ItemColor, Converter={StaticResource ColorConverter}}" Style="{StaticResource posttitle}" d:LayoutOverrides="Width"/>
Here's the converter which works during the initial render:
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);
}
During the SelectionChanged Event I assigned a new color to the item like this:
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 };
}
}
If I put a breakpoint and check the datacontext, I can see that the value in the source has changed but visually the LongListSelector
is not changing the look. The binding is to an ObservableCollection
and ItemColor
does notify of the change.
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供足够的信息,但根据您提供的代码,设置
item.ItemColor
时,ItemColor
的 PropertyChanged 事件似乎没有被触发。MyItem
应实现INotifyPropertyChanged
并调用PropertyChanged(this, new PropertyChangedEventArgs("ItemColor"))
。You have not given enough information, but based on the code you provided, it looks like when
item.ItemColor
is set, the PropertyChanged event forItemColor
is not being fired.MyItem
should implementINotifyPropertyChanged
and callPropertyChanged(this, new PropertyChangedEventArgs("ItemColor"))
.