Search code examples
wpfdatagrid

WPF DataGrid - IsSelected not overriding DataTrigger


I have a datagrid with a column as below

 <DataGrid  IsEnabled="True" SelectedItem="SelectedRow" FontSize="14"  Width="Auto"                             
                                   Height="450" AutoGenerateColumns="False" CanUserAddRows="False"
                                   HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch"
                                   x:Name="lstColumns" ItemsSource="{Binding Path=Rows}" >
                            <DataGrid.Resources>
                                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                            </DataGrid.Resources>
                            <DataGrid.RowStyle>
                                <Style TargetType="DataGridRow">
                                    <Style.Triggers>
                                        <MultiDataTrigger>
                                            <MultiDataTrigger.Conditions>
                                                <Condition Binding="{Binding ViewPermission}" Value="True" />
                                                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True" />
                                            </MultiDataTrigger.Conditions>
                                            <Setter Property="Background" Value="Green" />
                                            <Setter Property="Foreground" Value="White" />
                                        </MultiDataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </DataGrid.RowStyle>
                            <DataGrid.Columns>
                                <DataGridTemplateColumn CanUserSort="True" Width="200"  Header="Company Name" SortMemberPath="ClientName">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <ItemContainerTemplate>

                                            <TextBlock TextDecorations="Underline" Text="{Binding Path=ClientName}">
                                                <TextBlock.Style>
                                                    <Style TargetType="TextBlock">
                                                        <Style.Triggers>
                                                            <!--<MultiDataTrigger>
                                                                <MultiDataTrigger.Conditions>
                                                                    <Condition Binding="{Binding ViewPermission}" Value="True" />
                                                                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True" />
                                                                </MultiDataTrigger.Conditions>
                                                                <Setter Property="Background" Value="Green" />
                                                                <Setter Property="Foreground" Value="White" />
                                                            </MultiDataTrigger>-->
                                                            <DataTrigger Binding="{Binding ViewPermission}" Value="False">
                                                                <Setter Property="IsEnabled" Value="False"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding ViewPermission}" Value="False">
                                                                <Setter Property="Foreground" Value="Gray"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding ViewPermission}" Value="True">
                                                                <Setter Property="FontWeight" Value="Bold"/>
                                                                <Setter Property="Foreground" Value="#0078d4" />
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </TextBlock.Style>

                                                <TextBlock.InputBindings>
                                                    <MouseBinding MouseAction="LeftClick" Command="{Binding Path=DataContext.RowClick,  RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}"></MouseBinding>
                                                </TextBlock.InputBindings>
                                            </TextBlock>
                                        </ItemContainerTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                    <DataGridTemplateColumn.HeaderStyle>
                                        <Style TargetType="DataGridColumnHeader">
                                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                                        </Style>
                                    </DataGridTemplateColumn.HeaderStyle>
                                    </DataGridTemplateColumn>
.....
.....
</DataGrid>

I want the foreground set based on 'ViewPermissions'....but on RowClick/IsSelected, I want to apply different styles....

As per this code, the background 'Green' is getting applied on IsSelected.....but the foreground 'White' is not overriding the Column DataTrigger.....


Solution

  • Identified that in DataGrid.RowStyle, the TargetTYpe is DAtaGridRow while the other style property I used on the Column is of TargetType TextBlock.

    So removed the <DataGrid.RowStyle> and added the MultiDataTrigger to the TextBlock with Condition RelativeSource as DataGridRow,IsSelected.....

     <MultiDataTrigger>
                                                                        
     <MultiDataTrigger.Conditions>
    
     <Condition Binding="{Binding ViewPermission}" Value="True" />
                                                                            
     <Condition Binding="{Binding RelativeSource={RelativeSource 
      AncestorType=DataGridRow}, Path=IsSelected}" Value="True" />
                                                                        
     </MultiDataTrigger.Conditions>
                                                                        
     <Setter Property="Foreground" Value="White" />
    </MultiDataTrigger>
    

    This worked.....