【问题标题】:How to bind the DataGrid.HighlightBrush by row in WPF如何在 WPF 中按行绑定 DataGrid.HighlightBrush
【发布时间】:2012-08-24 13:02:24
【问题描述】:

我有一个数据网格,行的颜色不同,具体取决于其中记录的状态(有效为白色,问题为金色,禁止为红色)。

问题是选择行时,它们都转动均匀颜色,并且不可能确定状态。我想以与此类似的方式绑定突出显示颜色:

            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="{Binding Member, Converter={StaticResource MemberBackgroundConverter}}" />
                    <Setter Property="HighlightBrushKey" Value="{Binding Member, Converter={StaticResource MemberHighlightConverter}}" />                        
                </Style>
            </DataGrid.RowStyle>

上面的第一个 Setter 有效。有没有办法让第二个工作?有什么方法可以设置每行的 HighlightBrush 吗?

编辑: 以下是我目前的工作。我并不是说这是最好的方法,只是这种方法行得通。

XAML:

<DataGrid.Resources>
    <SolidColorBrush x:Key="SelectionBackgroundColorKey" />

    <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="0.0" />
        <GradientStop Color="White" Offset="0.3" />
        <GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="1.0" />
    </LinearGradientBrush>

    <SolidColorBrush x:Key="SelectionTextColorKey" Color="Black" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding Source={StaticResource SelectionTextColorKey}, Path=Color}" />
</DataGrid.Resources>

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="Background" Value="{Binding BindsDirectlyToSource=True, Converter={StaticResource ReservationBackgroundConverter}}" />
        <Setter Property="Foreground" Value="{Binding IsNew, Converter={StaticResource IsNewForegroundConverter}}" />
    </Style>
</DataGrid.RowStyle>

代码:

    private void DataGridReservationsSelectionChanged(object argSender, SelectionChangedEventArgs argEvtArgs)
    {
        Reservation LocalReservation;

        ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.SlateGray;
        ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;

        LocalReservation = dataGridReservations.SelectedItem as Reservation;

        if (LocalReservation == null)
        {
            return;
        }

        if(LocalReservation.IsArrived)
        {
            ((SolidColorBrush)dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.ForestGreen;
            ((SolidColorBrush)dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
            return;
        }
        //Is this Reservation a Problem?
        if (LocalReservation.Member.IsProblem)
        {
            ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Goldenrod;
            ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
            return;
        }

        //Is this Reservation Banned?
        if (LocalReservation.Member.IsBanned)
        {
            ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Firebrick;
            ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
            return;
        }
    }

这种方法允许我为每个独立的行设置未选择的颜色,并为每个独立的行设置选定的颜色。

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    如果使用 DataGridTextColumn,您可以指定 ElementStyle:

    <DataGrid.Resources>
        <Style x:Key="MemberCellStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="{Binding Member, Converter={StaticResource MemberBackgroundConverter}}" />
        </Style>
    </DataGrid.Resources>
    

    ...

     <DataGridTextColumn ... ElementStyle="{StaticResource MemberCellStyle}">
    

    无论是否选择了行,背景颜色都将遵循绑定。

    更新:

    您可以使用 MultiDataTrigger 并创建 2 个条件(1 个用于您的状态,1 个用于该行是否被选中)。这是一个在代码中创建样式的示例(针对选定和未选定条件),但如果需要,它应该很容易转换为 XAML:

    Style cellStyle = new Style( typeof( DataGridCell ) );
    MultiDataTrigger triggerNotSelected = new MultiDataTrigger();
    Condition dataCondition = new Condition( new Binding( "[STATUS]", [STATUSVALUE] );
    Condition rowNotSelectedCondition = new Condition();
    rowNotSelectedCondition.Binding = new Binding
         {
              Path = new PropertyPath( DataGridRow.IsSelectedProperty ),
              RelativeSource = new RelativeSource( RelativeSourceMode.FindAncestor, typeof( DataGridRow ), 1 )
         };
    rowNotSelectedCondition.Value = false;
    
    triggerNotSelected.Conditions.Add( dataCondition );
    triggerNotSelected.Conditions.Add( rowNotSelectedCondition );
    triggerNotSelected.Setters.Add( new Setter( Control.BackgroundProperty, Brushes.Gold ) );
    
    MultiDataTrigger triggerSelected = new MultiDataTrigger();
    Condition rowSelectedCondition = new Condition();
    rowSelectedCondition.Binding = new Binding
        {
             Path = new PropertyPath( DataGridRow.IsSelectedProperty ),
             RelativeSource = new RelativeSource( RelativeSourceMode.FindAncestor, typeof( DataGridRow ), 1 )
        };
    rowSelectedCondition.Value = true;
    
    triggerSelected.Conditions.Add( dataCondition );
    triggerSelected.Conditions.Add( rowSelectedCondition );
    triggerSelected.Setters.Add( new Setter( Control.BackgroundProperty, Brushes.Khaki ) );
    
    cellStyle.Triggers.Add( triggerNotSelected );
    cellStyle.Triggers.Add( triggerSelected );
    

    【讨论】:

    • 这不是我要找的。例如,未选中的行是 LightRed,所以我希望选中的行是 BrickRed。我希望颜色改变,但仍然有助于确定状态。我遇到的问题是行的颜色不同(红色、绿色和蓝色),因此每行的选择颜色需要不同。
    • 更新了答案。让我知道这是否是你要找的。​​span>
    • 如果我能让绑定工作,这看起来最有希望。这是我得到的,但它不起作用: 未选择的工作正常.事实上,您甚至不需要指定 IsSelected=false。
    • 您可能想尝试指定 AncestorLevel 并将路径更改为“IsSelected”而不是“IsSelectedProperty”... Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:类型 DataGridRow},AncestorLevel=1},Path=IsSelected}"
    • 我尝试了这两种方法,但选定的背景仍然没有改变。我认为问题在于我没有设置正确的属性。请参阅"How to change the blue color of selected datagrid rows" 了解更多信息。我将发布我的工作示例,看看我们是否可以对其进行编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    相关资源
    最近更新 更多