【问题标题】:Wpf ListViewItem Background binding to enumWpf ListViewItem 背景绑定到枚举
【发布时间】:2011-01-30 07:19:00
【问题描述】:

我有一个绑定到 ObservableCollection mPersonList 的 ListView。 Class Person 有一个枚举 Sex。我想要做的是,如果这个人是男性,则将 ListViewItem 的背景设置为绿色,如果这个人是女性,则将其设置为红色。

感谢您的回答!

我试过这样但是有什么问题?

<Style x:Key="CustomListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="{Binding Path=Status, Converter={StaticResource sexEnumToColor}}" />
</Style>

和:

public class SexEnumToColor : IValueConverter
{
    #region IValueConverter Member

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Sex tempSex = (Sex)value;
        Brush retval;

        switch (tempSex )
        {
            case Sex.Male:
                retval = Brushes.Blue;
                break;

            case Sex.Female:
                retval = Brushes.Red;
                break;

            default:
                retval = Brushes.Black;
                break;
        }

        return retval;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

【问题讨论】:

    标签: wpf listview binding enums


    【解决方案1】:

    在 Person 类的模板中使用 DataTrigger。

    如果您发布部分类定义和当前模板,我们可以完成它。

    【讨论】:

      【解决方案2】:

      您可以将性属性与转换器绑定。像这样的:

      <ListView>
          <ListView.ItemTemplate>
              <DataTemplate>
                  <TextBox Text="{Binding name}" Background="{Binding sex, Converter={StaticResource converterSexToColor}}" />
              </DataTemplate>
          </ListView.ItemTemplate>
      </ListView>
      

      和转换器:

      public class ConverterSexToColor : IValueConverter
      {
          public object Convert(object value, Type targeTtype, object parameter, System.Globalization.CultureInfo culture)
          {
              return (Sex)value == Sex.Male ? Brushes.Green :  Brushes.Red;
          }
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
      

      【讨论】:

      • 感谢那个答案,但这会改变文本框的背景颜色。如何更改一个列表视图项的背景颜色?
      • 在这种情况下,文本框只是数据模板中的一个元素。所以修改文本框的bg和修改listviewitem的bg是一样的。如果你有另一个数据模板,你可以用 包装你的模板(例如)并将性别属性绑定到它的背景。
      【解决方案3】:

      如果您不使用数据模板,则将绑定显式添加到 ListViewItem 的数据模板将不起作用。在这种情况下,您需要让样式发挥作用,并且要让它发挥作用,您需要知道为什么它不起作用。

      你的风格有什么问题是你给它分配了一个键。创建任何元素时,WPF 会在资源字典中搜索 Style 对象,该对象的键是该元素的类型。如果它找到一个,那就是它应用的样式。如果没有,则不会应用任何样式。

      如果您在样式声明中指定TargetType 但省略x:Key,则在将其添加到资源字典时分配的键就是该类型。但是,由于您已经为您的样式明确指定了一个键,所以这就是被分配的键。所以 WPF 永远找不到它,也没有得到应用。

      由于您不想将这种样式全局添加到窗口中的每个ListViewItem,要做的事情可能是将其添加到ListView 的资源字典中:

      <ListView ItemsSource="{DynamicResource Data}">
          <ListView.Resources>
              <local:SexToColorConverter x:Key="SexConverter" />
              <Style TargetType="{x:Type ListViewItem}">
                  <Setter Property="Background"
                          Value="{Binding Path=Sex, Converter={StaticResource SexConverter}}" />
              </Style>
          </ListView.Resources>
          <ListView.View>
              <GridView>
                  <GridViewColumn Header="Name"
                                  DisplayMemberBinding="{Binding Name}" />
                  <GridViewColumn Header="Sex"
                                  DisplayMemberBinding="{Binding Sex}" />
              </GridView>
          </ListView.View>
      </ListView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-03
        • 1970-01-01
        • 2013-08-30
        • 2016-03-26
        • 2011-05-06
        • 1970-01-01
        • 1970-01-01
        • 2011-06-09
        相关资源
        最近更新 更多