WPF DataGrid-在文本之外的FullRow选择上,SelectedItem不变

时间:2018-12-07 21:40:43

标签: c# wpf datagrid wpfdatagrid

我正在构建一个Datagrid,它具有高度为40的行。在选择行时,程序将打开一个新选项卡,其中包含特定于该项目的用户控件。

当我单击单元格内容(Red boxes in this image)之外行中的任意位置时,事件将触发,但'CurrentItem'不会更新。

基本上,仅当单击单元格内容时才选择该行。还有其他人处理过这个问题,并且对发生的事情有解决方案吗?

这是我的风格:

        <DataGrid Name="MachinesGrid" ItemsSource="{Binding MainData.Machines}">
        <DataGrid.Style>
            <Style TargetType="DataGrid">
                <Setter Property="SelectionMode" Value="Single" />
                <Setter Property="SelectionUnit" Value="FullRow" />
                <Setter Property="AutoGenerateColumns" Value="False" />
                <Setter Property="ColumnWidth" Value="Auto" />
                <Setter Property="CanUserAddRows" Value="False" />
                <Setter Property="Background" Value="{StaticResource BrushBackground}" />
                <Setter Property="RowBackground" Value="{StaticResource BrushGreyMid}" />
                <Setter Property="AlternatingRowBackground" Value="{StaticResource BrushGreyDark}" />
                <Setter Property="GridLinesVisibility" Value="None" />
                <Setter Property="ColumnHeaderStyle">
                    <Setter.Value>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="Background" Value="{StaticResource BrushBlue}" />
                            <Setter Property="Foreground" Value="{StaticResource BrushWhite}" />
                            <Setter Property="Height" Value="40" />
                            <Setter Property="FontWeight" Value="SemiBold" />
                        </Style>
                    </Setter.Value>
                </Setter>
                <Setter Property="CellStyle">
                    <Setter.Value>
                        <Style TargetType="DataGridCell">
                            <Setter Property="VerticalAlignment" Value="Center" />
                            <Setter Property="Foreground" Value="{StaticResource BrushWhite}" />
                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                        </Style>
                    </Setter.Value>
                </Setter>
                <Setter Property="RowStyle">
                    <Setter.Value>
                        <Style TargetType="DataGridRow">
                            <Setter Property="Height" Value="40" />
                            <EventSetter Event="MouseLeftButtonUp" Handler="EventSetter_OnHandler" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="{StaticResource BrushGreyLite}" />
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Background" Value="{StaticResource BrushBlue}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Style>

1 个答案:

答案 0 :(得分:0)

据我所知,您的问题是通过设置

最初在DataGridCell中触发了选择
<Setter Property="VerticalAlignment" Value="Center" />

您正在告诉DataGridCell应该在DataGridRow内垂直居中,这会导致顶部和底部的某个空间不属于DataGridCell,因此不会触发任何选择。

要解决此问题,您必须将VerticalAlignment设置为Stretch(默认),但是您的内容不再居中,为此,您必须覆盖{{1 }}:

ControlTemplate

现在,您可以在DataGridCell上设置<ControlTemplate TargetType="{x:Type DataGridCell}"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Border> </ControlTemplate> VerticalContentAlignment来对齐内容。


您的HorizontalContentAlignment-样式应如下所示

DataGridCell