WPF DataGrid - IsSelected未覆盖DataTrigger

xt0899hw  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(177)

我有一个数据网格,它的列如下所示

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

我希望前景设置基于'ViewPermissions'....但在RowClick/IsSelected,我想应用不同的样式....
根据此代码,背景“绿色”将应用于IsSelected.....但前景“白色”未覆盖Column DataTrigger.....

xurqigkl

xurqigkl1#

确定在DataGrid.RowStyle中,TargetTYpe是DAtaGridRow,而我在Column上使用的另一个样式属性是TargetTypeTextBlock。
因此,删除<DataGrid.RowStyle>并将MultiDataTrigger添加到TextBlock中,条件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>

这个工作……

相关问题