2013-06-25 30 views
0
<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文件中包含上述代碼。但觸發器功能從不觸發細胞聚焦。誰能解釋爲什麼會發生這種情況?ControlTemplate.Trigger從未在Datagrid上觸發Cell Focus

回答

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瞭解更多信息。

+0

是的,它在數學上起作用。 – neo