【问题标题】:Error by clicking on headers Datagrid WPF C#单击标题 Datagrid WPF C# 时出错
【发布时间】:2017-11-24 19:29:09
【问题描述】:

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

这是结果:

这是 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>

            //..

我该如何解决? 谢谢,

【问题讨论】:

  • 你为什么不发布一些代码?
  • 对不起,代码都是正确的,因为我没有发布

标签: c# wpf datagrid code-behind


【解决方案1】:

定义一个RowStyle,使用DataTriggers 设置Background

<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">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsOverFilled}" Value="True">
                    <Setter Property="Background" Value="Orange" />
                </DataTrigger>
                <DataTrigger Binding="{Binding IsOverWeighed}" Value="True">
                    <Setter Property="Background" Value="Orange" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    <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>
    ...
</DataGrid>

不要使用代码隐藏。

【讨论】:

  • 感谢您的帮助,但我有一个问题:如果我有 3 行要着色.. * 当我使用后面的代码时:总是正确的:3 行是着色的 * 当我使用Xaml 中的 DataTriggers,有时我有 2 个,有时有 3 个 .. 它不稳定,谢谢,
  • 使用数据触发器比使用代码隐藏更稳定。确保您在 XAML 中正确拼写了属性名称。
  • 非常感谢,我犯了一个错误..它现在是正确的,没有代码隐藏非常感谢,
猜你喜欢
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
相关资源
最近更新 更多