英文:
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.....
<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.....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论