WPF DataGrid – IsSelected不覆盖DataTrigger

huangapple go评论63阅读模式
英文:

WPF DataGrid - IsSelected not overriding DataTrigger

问题

我想根据 'ViewPermissions' 来设置前景色,但在 RowClick/IsSelected 时,我想应用不同的样式。

根据这段代码,背景色 'Green' 在 IsSelected 时被应用,但前景色 'White' 不能覆盖列的 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.....

答案1

得分: 0

在DataGrid.RowStyle中,TargetType为DataGridRow,而我在列上使用的另一个样式属性的TargetType为TextBlock。

因此,删除了<DataGrid.RowStyle>,并向TextBlock添加了MultiDataTrigger,条件为RelativeSource为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>

这个方法有效......

英文:

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

 &lt;MultiDataTrigger&gt;
                                                                    
 &lt;MultiDataTrigger.Conditions&gt;

 &lt;Condition Binding=&quot;{Binding ViewPermission}&quot; Value=&quot;True&quot; /&gt;
                                                                        
 &lt;Condition Binding=&quot;{Binding RelativeSource={RelativeSource 
  AncestorType=DataGridRow}, Path=IsSelected}&quot; Value=&quot;True&quot; /&gt;
                                                                    
 &lt;/MultiDataTrigger.Conditions&gt;
                                                                    
 &lt;Setter Property=&quot;Foreground&quot; Value=&quot;White&quot; /&gt;
&lt;/MultiDataTrigger&gt;

This worked.....

huangapple
  • 本文由 发表于 2023年6月6日 10:49:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定