Domanda

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

I have included above code in App.xaml file. But the trigger function never triggers on DataGrid cell focus. Can anyone explain why this happens?

È stato utile?

Soluzione

Try use FocusVisualStyle. With it, you can set the focus frame and additional values.

Sample:

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

Focuses are different. See the link for more information.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top