WPF 绑定:根据属性设置列表框项文本颜色 [英] WPF binding: Set Listbox Item text color based on property

查看:44
本文介绍了WPF 绑定:根据属性设置列表框项文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这可能是 WPF 中的一些基本知识,但我是 XAML 语法的新手,我正试图围绕它.

I'm sure this is probably something basic in WPF but I'm new to XAML syntax I'm trying to wrap my head around it.

我有一个 LogItem 类型——只是一个 POCO:

I have a LogItem Type -- just a POCO:

public class LogItem
{ 
    public string Message {get;set;}
    public Color MessageColor {get;set;}
}

和我的 ViewModel 中的 LogItem 列表:

and a List of LogItem in my ViewModel:

    private ObservableCollection<LogItem> _logItems; 
    public ObservableCollection<LogItem> LogItems
    {
        get { return _logItems; }
        set
        {
            if (value != _logItems)
            {
                _logItems = value;
                OnPropertyChanged("LogItems");
            }
        }
    }

我的视图模型绑定到视图,以便我可以执行以下操作:

My viewmodel is bound to the view so that I can do the following:

<ListBox Grid.Row="0" Margin="0,10,0,0" Grid.ColumnSpan="3" Height="150" ItemsSource="{Binding LogItems}">

(显然我还要设置显示文本绑定等)

(Obviously I still have to set the display text binding, etc.)

鉴于我在 LogItems 中有一个 MessageMessageColor 属性,将项目文本的颜色绑定到我指定的颜色的正确 XAML 语法是什么?

Given that I have a Message and MessageColor property in LogItems, what is the correct XAML syntax to bind the color of the item text to the color I specify?

推荐答案

    <ListBox Grid.Row="0" Margin="0,10,0,0" Grid.ColumnSpan="3" Height="150" ItemsSource="{Binding LogItems}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Message}" Foreground="{Binding MessageColor}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

TextBlock Foreground 需要 Brush 而不是 Color.就像 WPF 中的很多东西一样,有很多方法可以解决这个问题.这是一对夫妇:

TextBlock Foreground expects a Brush not a Color. Like a lot of things in WPF, There are lot's of ways to approch this. Here is a couple:

  1. 将 viewModel 中的 MessageColor 属性更改为 Brush

Brush MessageColor {get;set;}

  • 创建一个 SolidColorBrush 并将其绑定到您的颜色

  • Create a SolidColorBrush and bind it to your color

      <TextBlock Text="{Binding Message}">
          <TextBlock.Foreground>
             <SolidColorBrush Color="{Binding MessageColor}"/>
          </TextBlock.Foreground>
      </TextBlock>
    

  • 创建一个ColorToBrushConverter

    public class ColorToBrushConverter : IValueConverter
    {
          #region IValueConverter Members
    
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
                 if (value == null) return Brushes.Black; // Default color
    
                 Color color = (Color)value;
    
                 return new SolidColorBrush(color);
          }
    
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
                 throw new NotImplementedException();
          }
    
          #endregion
    }
    

  • 在 xaml 中,将转换器创建为静态资源

    In xaml, create the converter as static resource

    <Window.Resources>
        <local:ColorToBrushConverter x:Key="colorToBrushConverter"/>
    </Window.Resources>
    

    在绑定中使用它

    <TextBlock Text="{Binding Message}" Foreground="{Binding MessageColor, Converter={StaticResource colorToBrushConverter}"/>
    

    祝你好运

    这篇关于WPF 绑定:根据属性设置列表框项文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆