C# WPF Datagrid 基于值触发行颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18580612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:38:10  来源:igfitidea点击:

WPF Datagrid trigger row colour based on value

c#wpfdatagrid

提问by mHelpMe

I have a WPF application that contains a datagrid. The datagrid is bound to my object OrderBlock which contains a List of type Orders.

我有一个包含数据网格的 WPF 应用程序。数据网格绑定到我的对象 OrderBlock,其中包含一个类型为 Orders 的列表。

<DataGrid DataContext="{Binding OrderBlock}"
              Name="dataGridOrdersGood" 
              ItemsSource="{Binding Orders}"

This works fine and displays nicely in my datagrid. There is one property (StatusGood) in my List though that I would like to display as a combobox where there can be only two values, "Send" or "Hold".

这工作正常,并在我的数据网格中很好地显示。我的列表中有一个属性 (StatusGood),但我想显示为一个组合框,其中只能有两个值,“发送”或“保留”。

If the value in the combobox is "Hold" I would like the row to turn different colour. Ideally using a linear gradient going from silver to yellow. I have tried the code below - literally just trying to turn the row red for the moment but nothing happens. I can't see what is wrong with my code below. The trigger part is very close to the bottom of the code below. I'm new to WPF and struggling with it at the moment. The code below has mainly come from a very good post that can be found here, http://www.codeproject.com/Articles/586132/WPF-DataGrid-Custommization-using-Style-and-Templa

如果组合框中的值为“Hold”,我希望该行变成不同的颜色。理想情况下使用从银色到黄色的线性渐变。我已经尝试了下面的代码 - 实际上只是暂时尝试将行变成红色,但没有任何反应。我看不出我下面的代码有什么问题。触发部分非常靠近下面代码的底部。我是 WPF 的新手,目前正在努力解决它。下面的代码主要来自一个非常好的帖子,可以在这里找到,http://www.codeproject.com/Articles/586132/WPF-DataGrid-Custommization-using-Style-and-Templa

    <!-- Data grid formatting Grid Row template -->
    <Style x:Key="DG_Row" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="LightGreen"/>
        <Setter Property="Opacity" Value="1"/>
        <Setter Property="Padding" Value="3,2,2,3"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>            
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="True">
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Offset="0" Color="Transparent"/>
                                <GradientStop Offset="1" Color="Silver"/>
                            </LinearGradientBrush>                                
                        </Border.Background>
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="20" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter Grid.Column="1"
                                ItemsPanel="{TemplateBinding ItemsPanel}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <DataGridDetailsPresenter Grid.Row="1"
                                Grid.Column="1"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical},
                                  Converter={x:Static DataGrid.RowDetailsScrollingConverter},
                                  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                Visibility="{TemplateBinding DetailsVisibility}" />
                                                    <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
                                                        Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row},
                                                                        Converter={x:Static DataGrid.HeadersVisibilityConverter},
                            RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                        </SelectiveScrollingGrid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Normal_AlternatingRow">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" 
                                            Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                            <EasingColorKeyFrame KeyTime="0" Value="#AAF0C570" />
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" 
                                            Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                            <EasingColorKeyFrame KeyTime="0" Value="#AAFF7F00" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Normal_Selected">
                                    <Storyboard>
                                        <!-- ColorAnimation here same as Normal_AlternatingRow state -->
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <!-- ColorAnimation here same as Normal_AlternatingRow state -->
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Border>                        
                </ControlTemplate>
            </Setter.Value>
          <Style.Triggers>
              <DataTrigger Binding="{Binding Active}" Value="Hold">
                 <Setter Property="Background" Value="Red" />
             </DataTrigger>
        </Style.Triggers>
        </Setter>                
    </Style>

As always any help would be great. Thanks M

与往常一样,任何帮助都会很棒。谢谢米

采纳答案by Chris

Do you need to change the behaviour of the DataGridRow, or is it sufficient to alter the style?

你需要改变 的行为DataGridRow,还是改变风格就足够了?

If changing the row highlighting based on a property is all you need, you should be able to just use a simpler Style, something like this:

如果您只需要根据属性更改行突出显示,您应该可以使用更简单的Style,如下所示:

    <!-- A brush -->
    <LinearGradientBrush x:Key="BgBrush1" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Offset="0" Color="#888888"/>
        <GradientStop Offset="1" Color="#FFFFF86E"/>
    </LinearGradientBrush>

    <!-- Your row style -->
    <Style x:Key="HighlightRow" TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding StatusGood}" Value="Hold">
                <Setter Property="Background" Value="{StaticResource BgBrush1}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

You should be able to apply the style when required in a DataGridby using your style as a StaticResourcefor the RowStyleproperty:

在需要时,你应该能够应用的样式DataGrid用自己的风格作为StaticResourceRowStyle属性:

<DataGrid DataContext="{Binding OrderBlock}"
          Name="dataGridOrdersGood" 
          ItemsSource="{Binding Orders}" 
          RowStyle="{StaticResource HighlightRow}" />

Edit:

编辑:

If you want to retain the rest of your styling and use a control template, you can place your DataTriggerin your ControlTemplate.Triggers, you'll also have to supply a TargetNameproperty, to specify the element you wish the trigger to act on, so using my above brush, and your initial code:

如果你想留住你的风格的休息和使用控制模板,你可以把你 DataTrigger在你的ControlTemplate.Triggers,你还必须提供一个TargetName属性,指定希望触发作用,所以用我上面刷元素,以及您的初始代码:

<!-- Data grid formatting Grid Row template -->
<Style x:Key="DG_Row" TargetType="{x:Type DataGridRow}">        
    <Setter Property="Template">            
        <Setter.Value>
        <!-- Your code -->
            <ControlTemplate TargetType="{x:Type DataGridRow}">
                <Border x:Name="DGR_Border"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="True">
                <!-- Your code -->
                </Border>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding StatusGood}" Value="Send">
                        <Setter TargetName="DGR_Border" Property="Background" Value="{StaticResource BgBrush1}"/>
                     </DataTrigger>
                 </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Where DGR_Borderis the name you had given your border with the existing gradient.

DGR_Border您使用现有渐变为边框命名的名称在哪里。