Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在WPF中按行绑定DataGrid.HighlightBrush_Wpf_Binding - Fatal编程技术网

如何在WPF中按行绑定DataGrid.HighlightBrush

如何在WPF中按行绑定DataGrid.HighlightBrush,wpf,binding,Wpf,Binding,我有一个datagrid,根据其中记录的状态,行是不同的颜色(有效的是白色,问题是金色,禁止的是红色) 问题是,当选择行时,它们都会变成统一的颜色,因此无法再确定状态。我希望以与此类似的方式绑定高光颜色: <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="Background" V

我有一个datagrid,根据其中记录的状态,行是不同的颜色(有效的是白色,问题是金色,禁止的是红色)

问题是,当选择行时,它们都会变成统一的颜色,因此无法再确定状态。我希望以与此类似的方式绑定高光颜色:

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

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

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

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

那不是我要找的。例如,未选中的行是浅红色的,因此我希望选中的行是砖砌的。我希望颜色可以更改,但仍有助于确定状态。我遇到的问题是,行是不同的颜色(红色、绿色和蓝色),因此每行的选择颜色需要不同。更新答案。让我知道这是否是你想要的。如果我能让绑定工作,这看起来是最有希望的。这就是我得到的,但它不起作用:未经选择的运行良好。事实上,您甚至不需要指定IsSelected=false。您可以尝试指定AncestorLevel并将路径更改为“IsSelected”而不是“IsSelectedProperty”。。。Binding=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow},AncestorLevel=1},Path=IsSelected}”我尝试了这两种方法,但所选背景仍然没有改变。我认为问题在于我没有设置正确的属性。有关更多信息,请参阅。我将发布一个我的工作样本,看看我们是否可以编辑它。
    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;
        }
    }
<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}">
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 );