如何使用数据绑定基于属性值设置DataGrid的行Background

Tob*_*sen 66 wpf xaml wpfdatagrid

在我的XAML代码中,我想Background根据一个特定行中对象的值设置每一行的颜色.我有一个ObservableCollectionz,并且每个z有一个叫做财产State.我开始用这样的东西在我的DataGrid:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="Background" 
                Value="{Binding z.StateId, Converter={StaticResource StateIdToColorConverter}}"/>
     </Style>
</DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)

这是一种错误的方法,因为x不是我的ViewModel类中的属性.

在我的ViewModel类我有一个ObservableCollection<z>是在ItemsSourceDataGrid,和SelectedItem类型z.

我可以绑定颜色SelectedItem,但这只会改变一行DataGrid.

我如何根据一个属性更改此行backgroundcolor?

Nit*_*esh 146

使用DataTrigger:

<DataGrid ItemsSource="{Binding YourItemsSource}">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow"> 
            <Style.Triggers>
                <DataTrigger Binding="{Binding State}" Value="State1">
                    <Setter Property="Background" Value="Red"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding State}" Value="State2">
                    <Setter Property="Background" Value="Green"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

  • 在从wpf离开之后再次遇到这个,希望我能再次投票! (4认同)
  • 这很棒.在我使用它的解决方案中,我需要根据`enum`值更改状态.[StackOverflow上的这个答案](http://stackoverflow.com/questions/13917033/datatrigger-on-enum-to-change-image)帮助了我. (4认同)
  • 我只得到:BindingExpression路径错误:在'对象'''z'(HashCode = 7162954)'上找不到''State'属性.BindingExpression:路径=状态; DataItem ='z'(HashCode = 7162954); target元素是'DataGridRow'(Name =''); target属性是'NoTarget'(类型'Object')`当我的实体拥有它时,它没有找到属性状态,并且我的数据库将State显示为列? (2认同)
  • 我希望你不要像`z.State`那样做. (2认同)

Vah*_*yan 15

同样可以没有DataTrigger:

 <DataGrid.RowStyle>
     <Style TargetType="DataGridRow">
         <Setter Property="Background" >
             <Setter.Value>
                 <Binding Path="State" Converter="{StaticResource BooleanToBrushConverter}">
                     <Binding.ConverterParameter>
                         <x:Array Type="SolidColorBrush">
                             <SolidColorBrush Color="{StaticResource RedColor}"/>
                             <SolidColorBrush Color="{StaticResource TransparentColor}"/>
                         </x:Array>
                     </Binding.ConverterParameter>
                 </Binding>
             </Setter.Value>
         </Setter>
     </Style>
 </DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)

BooleanToBrushConverter以下课程在哪里:

public class BooleanToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return Brushes.Transparent;

        Brush[] brushes = parameter as Brush[];
        if (brushes == null)
            return Brushes.Transparent;

        bool isTrue;
        bool.TryParse(value.ToString(), out isTrue);

        if (isTrue)
        {
            var brush =  (SolidColorBrush)brushes[0];
            return brush ?? Brushes.Transparent;
        }
        else
        {
            var brush = (SolidColorBrush)brushes[1];
            return brush ?? Brushes.Transparent;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在XAML中,为DataGrid添加并定义RowStyle属性,其目标是将RowBackground设置为在Employee对象中定义Color。

<DataGrid AutoGenerateColumns="False" ItemsSource="EmployeeList">
   <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
             <Setter Property="Background" Value="{Binding ColorSet}"/>
        </Style>
   </DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)

在我的员工班上

public class Employee {

    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }

    public string ColorSet { get; set; }

    public Employee() { }

    public Employee(int id, string name, int age)
    {
        Id = id;
        Name = name;
        Age = age;
        if (Age > 50)
        {
            ColorSet = "Green";
        }
        else if (Age > 100)
        {
            ColorSet = "Red";
        }
        else
        {
            ColorSet = "White";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,每一个DataGrid的行具有背景色的的ColorSet 我的对象的属性

  • 但这违反了 MVVM。如果你不在乎,那就去吧。但用户体验不应该由模型决定。这就是视图/视图模型的工作 (2认同)