【问题标题】:Label in WPF XAML DataGridTemplateColumn inactive selection styleWPF XAML DataGridTemplateColumn 非活动选择样式中的标签
【发布时间】:2020-07-27 21:02:41
【问题描述】:

我在 DataGridTemplateColumn 中有一个标签,我使用触发器确保在选择行时前景为白色(蓝色)。 选择行后,但没有聚焦DataGrid,它仍然是白色的;这是有道理的,因为它仍然被选中。

但我想在 IsSelected 和网格处于非活动状态时将前景设为黑色。

这里是样式和列

感谢您的帮助。

 <Style x:Key="DataGridLabelStyle" TargetType="Label" BasedOn="{StaticResource DataGridControlStyle}" >
                    <Setter Property="Foreground" Value="Black" />

                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding >
                                <MultiBinding Converter="{StaticResource BooleanAndConverter}">
                                    <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type DataGridRow}}" />
                                    <Binding Path="IsKeyboardFocused" RelativeSource="{RelativeSource AncestorType={x:Type DataGrid}}" Converter="{StaticResource NegateBooleanConverter}" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Foreground" Value="White" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>

                <DataGridTemplateColumn Header="Date">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding TransactionDate}" Style="{StaticResource DataGridLabelStyle}" ContentStringFormat="yyyy-MM-dd" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding TransactionDate}"  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

【问题讨论】:

    标签: wpf xaml triggers datagrid wpf-style


    【解决方案1】:

    您可以将MultiDataTrigger 与绑定到父DataGridCellSelector.IsSelectionActive 附加属性的Condition 一起使用:

    <Style x:Key="DataGridLabelStyle" TargetType="Label">
        <Setter Property="Foreground" Value="Black" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions >
                    <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}}" Value="True" />
                    <Condition Binding="{Binding (Selector.IsSelectionActive), RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}}" Value="True" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="White" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多