6

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

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

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

如果组合框中的值是“保持”,我希望该行变成不同的颜色。理想情况下使用从银色到黄色的线性渐变。我已经尝试了下面的代码 - 实际上只是暂时尝试将行变为红色,但没有任何反应。我看不到下面的代码有什么问题。触发部分非常接近下面代码的底部。我是 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>

一如既往,任何帮助都会很棒。谢谢米

4

1 回答 1

18

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

如果您只需要根据属性更改行突出显示,您应该可以使用更简单的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>

您应该能够在 a 中需要时应用该样式,DataGrid方法是将您的样式用作StaticResource属性RowStyle

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

编辑:

如果您想保留其余样式并使用控件模板,您可以将您 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>

DGR_Border您给与现有渐变边界的名称在哪里。

于 2013-09-02T21:36:46.377 回答