簡體   English   中英

更改綁定的LongListSelector或Listbox的項目顏色

[英]Changing an item color of a LongListSelector or Listbox that is binded

我一直在嘗試更改從綁定中獲取顏色的ListBoxTextBlock的顏色。

<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 };
    }
}

如果我放置一個斷點並檢查數據上下文,則可以看到源中的值已更改,但從外觀上看LongListSelector並沒有改變外觀。 綁定是綁定到ObservableCollection並且ItemColor會通知更改。

任何幫助表示贊賞。

您還沒有給予足夠的信息,但根據您所提供的代碼,它看起來像時item.ItemColor設置為PropertyChanged事件ItemColor沒有被解雇。

MyItem應該實現INotifyPropertyChanged並調用PropertyChanged(this, new PropertyChangedEventArgs("ItemColor"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM