Wpf DataGrid右“Gutter”被选中触发器

时间:2017-12-08 00:15:36

标签: c# wpf xaml datagrid

在Wpf DataGrid中,每一行都有一个空白区域,该区域会增长以填充剩余空间。我想这个区域更适合称为“排水沟”,但我实际上并不知道它的正确术语。我喜欢这个,但我发现它有问题。如果我单击空白区域,它似乎不会处理我在行上设置的IsSelected触发器。但是,IsMouseOver触发器被触发。很奇怪。这张照片展示了我提到的区域:

enter image description here

以下是我认为需要修复的Xaml部分。如果需要更多,请告诉我,我会发布它。

<!--  Style and template for the DataGridRow.  -->
    <Style x:Key="VoidwalkerDataGridRowStyle" TargetType="{x:Type DataGridRow}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock
                        Margin="2,0,0,0"
                        VerticalAlignment="Center"
                        Foreground="Red"
                        Text="!" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border
                        x:Name="DGR_Border"
                        Background="{DynamicResource VoidwalkerContextBrush}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="True">
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter
                                Grid.Column="1"
                                ItemsPanel="{TemplateBinding ItemsPanel}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            <DataGridDetailsPresenter
                                Grid.Row="1"
                                Grid.Column="1"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                Visibility="{TemplateBinding DetailsVisibility}" />
                            <DataGridRowHeader
                                Grid.Row="0"
                                Grid.RowSpan="2"
                                Grid.Column="0"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
                                Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                        </SelectiveScrollingGrid>

                    </Border>
                    <ControlTemplate.Triggers>
                        <!--
                            Alternation Coloration Triggers
                        -->
                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                            <Setter Property="Foreground" Value="{DynamicResource VoidwalkerForegroundBrush}" />
                            <Setter TargetName="DGR_Border" Property="Background" Value="{DynamicResource VoidwalkerContextBrush}" />
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter Property="Foreground" Value="{DynamicResource VoidwalkerForegroundBrush}" />
                            <Setter TargetName="DGR_Border" Property="Background" Value="{DynamicResource VoidwalkerControlBrush}" />
                        </Trigger>
                        <!--
                            Is Selected Triggers
                        -->
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="DGR_Border" Property="BorderBrush" Value="{DynamicResource VoidwalkerBorderBrush}" />
                            <Setter TargetName="DGR_Border" Property="Background" Value="{DynamicResource VoidwalkerBorderBrush}" />
                        </Trigger>

                        <!--
                            Is Mouse Over Triggers
                        -->
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="DGR_Border" Property="BorderBrush" Value="{DynamicResource VoidwalkerBorderBrush}" />
                            <Setter TargetName="DGR_Border" Property="Background" Value="{DynamicResource VoidwalkerBorderBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

进一步澄清我的问题:

当我点击图片中的“装订线”区域时,我的IsSelected触发器不会被触发。如果我单击它左侧的任何单元格,将选择该行。奇怪的是,当将鼠标悬停在排水沟区域时,我的另一个IsMouseOver触发器仍然被触发。有没有办法可以模拟排水沟区域上的IsSelected行为?

1 个答案:

答案 0 :(得分:1)

它只是可选择的实际单元格。你可以很容易地制作&#34; gutter&#34;只需向DataGrid添加另一个空白列即可选择:

<DataGrid.Columns>
    ...
    <DataGridTemplateColumn Width="*" />
</DataGrid.Columns>

或者通过将其Width设置为*,使最后一个现有列水平拉伸。