单击标题Datagrid WPF C#时出错

时间:2017-11-23 15:47:53

标签: c# wpf datagrid code-behind

我的XAML中有一个代码:当检查条件时,目标行将为橙色。

这是结果: enter image description here

这是XAML.cs中的代码:

     private void DataGrid_Loaded(object sender, RoutedEventArgs e)
    {
        foreach (TrainOrdersClass item in dgBaseProd.ItemsSource)
        {
            var row = dgBaseProd.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
            if ((item.IsOverFilled == true) || (item.IsOverWeighed == true))
            {
                row.Background = Brushes.Orange;
            }
        }
    }

问题是,当我点击DataGrid的标题时,所有颜色都消失了!

Xaml:

      <DataGrid   Name="DataGrid"   AutoGenerateColumns="False"  
              Height="Auto" Width="780" Margin="10,10,10,10"
              IsReadOnly="True" ItemsSource="{Binding Path=PreloadedRailcarstList}"
              SelectedItem="{Binding Path=BaseProductToUpdate}"
              AlternationCount="2"  AlternatingRowBackground="LightBlue" Loaded="DataGrid_Loaded"  >                                

                <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <i:InvokeCommandAction Command="{Binding Path=DataContext.OpenUpdateBaseProductViewCmd , RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding BaseProductToUpdate.name}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="PreviewKeyDown">
                    <i:InvokeCommandAction Command="{Binding Path=DataContext.OpenUpdateBaseProductViewCmd , RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding BaseProductToUpdate.name}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

            //..

我该如何解决? 感谢,

1 个答案:

答案 0 :(得分:1)

定义使用RowStyle设置Background的{​​{1}}:

DataTriggers

不要使用代码隐藏。

相关问题