不显示“在行中聚焦单元格”

时间:2015-11-04 14:03:04

标签: c# wpf datagrid triggers controltemplate

我的StyleDataGridRow有一个DataGridCell。在我的RowStyle中,我更改了所选Row的背景,在我的CellStyle中,我更改了我的焦点Cell的背景。这在我第一次点击新的Row时有效。但是,如果我点击同一Cell中的另一个Row,则不会应用Style。如果我使用我的箭头键更改Cell它可以正常工作,而不是我点击鼠标。在我的DataGridSelectionMode =单身。

<!--DataGridRow-->
    <Style x:Key="MYDGRowStyle" TargetType="{x:Type DataGridRow}">            
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <!--<Setter Property="OverridesDefaultStyle" Value="True" />-->           
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Grid x:Name="RowGridTop">
                        <Grid Grid.ZIndex="89" x:Name="RowGrid"/>
                            <Border
                                    Grid.ZIndex="88"
                                    x:Name="DGR_Border"
                                    Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <SelectiveScrollingGrid x:Name="selectiveScrollingGrid">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <!--<RowDefinition Height="Auto" />-->
                                    </Grid.RowDefinitions>
                                    <DataGridCellsPresenter x:Name="dataGridCellsPresenter" Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                            Focusable="True" />
                                    <DataGridRowHeader x:Name="dataGridRowHeader" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Grid.RowSpan="2"
                                        Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                                        ConverterParameter={x:Static DataGridHeadersVisibility.Row}}"/>
                                    <Grid Grid.Column="1" Grid.Row="1" Name="Details" Visibility="Collapsed">
                                        <DataGridDetailsPresenter x:Name="dataGridDetailsPresenter" />
                                    </Grid>
                                </SelectiveScrollingGrid>
                            </Border>                            
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">                                
                            <Setter Property="Visibility" TargetName="dataGridDetailsPresenter" Value="Hidden"/>
                            <Setter Property="Background" TargetName="RowGrid" Value="{DynamicResource Brush_DataGridSelected}" />                                
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>            
    </Style>
    <!--DataGridRow-->



    <!--DataGridCell-->
    <Style x:Key="MYDGCellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template" Value="{DynamicResource MYDGCellControlTemplate}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsReadOnly}" Value="True">
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="Gray" Opacity="0.3" />
                    </Setter.Value>
                </Setter>
            </DataTrigger>                
        </Style.Triggers>
    </Style>

    <ControlTemplate x:Key="MYDGCellControlTemplate" TargetType="{x:Type DataGridCell}">            
            <Grid>
            <Grid Grid.ZIndex="99"  x:Name="CellGrid"/>
            <Border Grid.ZIndex="98" Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
            </Grid>            
        <ControlTemplate.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Background" TargetName="CellGrid" Value="{DynamicResource Brush_DataGridCellFocused}" />                                     
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>        
    <!--DataGridCell-->

如果我在Trigger中定位'RowGridTop',则会正确显示焦点Cell,但ZIndex不正确。 Here显示了我需要的原因 额外的Grid

1 个答案:

答案 0 :(得分:0)

好的,我做到了。我不得不更改ZIndex,以便DataGridRowStyle上的那些高于DataGridCellStyle上的那个。我还必须在我的“RowGrid”IsHitTestVisible="False"上设置Grid