根据集合更改行颜色

时间:2013-12-30 08:00:55

标签: c# wpf datagrid

在我的订单DataGrid中,我需要在集合中多次更改其中客户存在的行的背景颜色。 enter image description here

在上面的DataGrid中,我希望Jimmy的订单以黄色显示,因为Jimmy有多个订单。
这就是我填充DataGrid

的方式
 var people = new List<Person>()
        {
            new Person{CustName = "Jimmy", CustNum = 1, OrderNumber = "1111", OrederDate = DateTime.Now.AddDays(Helpers.GetRandom(1,10))},
            new Person{CustName = "Alex", CustNum = 5, OrderNumber = "2222", OrederDate = DateTime.Now.AddDays(Helpers.GetRandom(1,10))},
            new Person{CustName = "Jimmy", CustNum = 1, OrderNumber = "3333", OrederDate = DateTime.Now.AddDays(Helpers.GetRandom(1,10))},
            new Person{CustName = "Robert", CustNum = 7, OrderNumber = "4444", OrederDate = DateTime.Now.AddDays(Helpers.GetRandom(1,10))}
        };

        dgOrders.ItemsSource = people;

XAML

<DataGrid x:Name="dgOrders" CanUserAddRows="False" CanUserDeleteRows="False" 
                      CanUserReorderColumns="True" AutoGenerateColumns="False"
                      VerticalScrollBarVisibility="Visible" Height="250">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Customer Number" Binding="{Binding CustNum}" Width="Auto"/>
            <DataGridTextColumn Header="Customer Name" Binding="{Binding CustName}" Width="*"/>
            <DataGridTextColumn Header="Order Number" Binding="{Binding OrderNumber}" Width="Auto"/>
            <DataGridTextColumn Header="Date" Binding="{Binding OrederDate, StringFormat=\{0:dd/MM/yyyy\}}" Width="Auto"/>
        </DataGrid.Columns>
    </DataGrid>

2 个答案:

答案 0 :(得分:1)

您可以将IMultiValueConverterDataGridRowStyle结合使用来实现。在转换器中传递DataGrid的ItemsSource和当前行的CustName属性。

转换器将检查ItemSource中是否存在多个具有相同CustName的项目,返回画笔要说Brushes.Green否则它将返回Brushes.Transparent

<DataGrid>
   <DataGrid.Resources>
       <local:ItemCountToBrushConverter x:Key="ItemCountToBrushConverter"/>
   </DataGrid.Resources>
    <DataGrid.RowStyle>
       <Style TargetType="DataGridRow">
          <Setter Property="Background">
             <Setter.Value>
                <MultiBinding Converter="{StaticResource ItemCountToBrushConverter}">
                    <Binding Path="ItemsSource"
                             RelativeSource="{RelativeSource Mode=FindAncestor,
                                                       AncestorType=DataGrid}"/>
                     <Binding Path="CustName"/>
                </MultiBinding>
             </Setter.Value>
          </Setter>
       </Style>
    </DataGrid.RowStyle>
    ....
</DataGrid>

转换器代码:

public class ItemCountToBrushConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
                          object parameter, CultureInfo culture)
    {
        if (values.Length == 2)
        {
            IEnumerable<Person> collection = (IEnumerable<Person>)values[0];
            int count = collection.Count(item => item.CustName ==
                                                  values[1].ToString());
            return (count > 1) ? Brushes.Green : Brushes.Transparent;
        }
        return Brushes.Transparent;
    }

    public object[] ConvertBack(object value, Type[] targetTypes,
                                object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

答案 1 :(得分:0)

或者将另一个属性添加到Person类(此处名为Mark)。如果希望视图突出显示该人,请在视图模型中将Mark设置为true。 XAML看起来像这样:

        <DataGrid x:Name="dgOrders" CanUserAddRows="False" CanUserDeleteRows="False" 
                  CanUserReorderColumns="True" AutoGenerateColumns="False"
                  VerticalScrollBarVisibility="Visible" Height="250">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Customer Number" Binding="{Binding CustNum}" Width="Auto"/>
            <DataGridTextColumn Header="Customer Name" Binding="{Binding CustName}" Width="*"/>
            <DataGridTextColumn Header="Order Number" Binding="{Binding OrderNumber}" Width="Auto"/>
            <DataGridTextColumn Header="Date" Binding="{Binding OrederDate, StringFormat=\{0:dd/MM/yyyy\}}" Width="Auto"/>
        </DataGrid.Columns>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Mark}" Value="True">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>