【问题标题】:ControlTemplate.Trigger never triggers on Datagrid Cell FocusControlTemplate.Trigger 从不触发 Datagrid Cell Focus
【发布时间】:2013-06-25 12:41:52
【问题描述】:
<Style x:Key="Body_Content_DataGrid_Centering" TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" />
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="BorderBrush" Value="DarkGray" />
                        <Setter Property="BorderThickness" Value="0.5" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>               
    </Setter>              
</Style>

我已在App.xaml 文件中包含上述代码。但是触发功能永远不会在DataGrid 单元格焦点上触发。谁能解释为什么会这样?

【问题讨论】:

    标签: wpf xaml wpf-4.0 app.xaml


    【解决方案1】:

    尝试使用 FocusVisualStyle。有了它,您可以设置焦点框和附加值。

    示例:

    <!-- CellFocusVisual -->
    <Style x:Key="CellFocusVisual" TargetType="{x:Type Control}">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Control}">
                   <Border SnapsToDevicePixels="True" CornerRadius="0" BorderThickness="2" BorderBrush="#7B2F81" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <!-- DataGridCell -->
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="BorderBrush" Value="{x:Null}" />
        <Setter Property="FocusVisualStyle" Value="{StaticResource CellFocusVisual}" />
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                     <Border x:Name="BackgroundBorder" BorderThickness="3" Background="Transparent">
                         <ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" />
                     </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    关注点不同。有关更多信息,请参阅link

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      相关资源
      最近更新 更多