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

发布于 2024-12-16 23:51:46 字数 1276 浏览 0 评论 0原文

我一直在尝试更改从绑定获取颜色的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-12-23 23:51:46

您没有提供足够的信息,但根据您提供的代码,设置 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 for ItemColor is not being fired.

MyItem should implement INotifyPropertyChanged and call PropertyChanged(this, new PropertyChangedEventArgs("ItemColor")).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文