简体   繁体   English

当 SelectionMode 设置为 Cell 时,如何突出显示 WPF DataGrid 中的一行

[英]How do I highlight a row in the WPF DataGrid when SelectionMode is set to Cell

I have a DataGrid bound to a DataTable in a WPF application.我有一个绑定到 WPF 应用程序中的DataTableDataGrid The SelectionUnit of the DataGrid has to be set to Cell, but I'd also like to add a subtle highlight to the whole row so that on wide DataGrids, when the user scrolls off the selected cell they can still see the row highlighted. DataGrid 的SelectionUnit必须设置为 Cell,但我还想为整行添加一个微妙的突出显示,以便在宽 DataGrids 上,当用户滚动离开所选单元格时,他们仍然可以看到突出显示的行。

This is a little problematic since the IsSelected property of the DataGridRow never gets set to true, because the Row isn't selected, the Cell is.这有点问题,因为 DataGridRow 的 IsSelected 属性永远不会设置为 true,因为没有选择 Row,而选择了 Cell。

I don't mind is it's a little arrow in a sticky RowSelector or a highlight applied to the whole Row.我不介意它是粘性 RowSelector 中的一个小箭头还是应用于整个 Row 的突出显示。 Just need some way of highlighting which row is selected.只需要某种方式突出显示选择了哪一行。

I've played a bit here, nothing spectacular but is a working version:我在这里玩了一点,没什么了不起的,但它是一个工作版本:

    <Style TargetType="DataGridCell">
       <EventSetter Event="Selected" Handler="EventSetter_OnHandlerSelected"/>
       <EventSetter Event="LostFocus" Handler="EventSetter_OnHandlerLostFocus"/>
    </Style>

This is the codebehind:这是代码隐藏:

    private void EventSetter_OnHandlerSelected(object sender, RoutedEventArgs e)
    {
        DataGridRow dgr = FindParent<DataGridRow>(sender as DataGridCell);
        dgr.Background = new SolidColorBrush(Colors.Red);
    }

    private void EventSetter_OnHandlerLostFocus(object sender, RoutedEventArgs e)
    {
        DataGridRow dgr = FindParent<DataGridRow>(sender as DataGridCell);
        dgr.Background = new SolidColorBrush(Colors.White);
    }

And this is the helper method to get the parent:这是获取父级的辅助方法:

    public static T FindParent<T>(DependencyObject child) where T : DependencyObject
    {
        //get parent item
        DependencyObject parentObject = VisualTreeHelper.GetParent(child);

        //we've reached the end of the tree
        if (parentObject == null) return null;

        //check if the parent matches the type we're looking for
        T parent = parentObject as T;
        if (parent != null)
            return parent;
        else
            return FindParent<T>(parentObject);
    }

It's not MVVM, but taking into consideration that we're just working with the View elements .. i think this time it's not a must.这不是 MVVM,但考虑到我们只是在处理 View 元素......我认为这一次不是必须的。 So basically, on first selection you color the row, and on lost focus turn back to white the previous one and change color for the new selection.所以基本上,在第一次选择时,你为行着色,失去焦点时将前一个变回白色,并为新选择改变颜色。

This works and is as MVVM as it gets.这很有效,并且与 MVVM 一样有效。 For better readability I used ExpressionConverter which is an amaizing way to make XAML more readable but more error prone.为了更好的可读性,我使用了ExpressionConverter ,这是一种使 XAML 更具可读性但更容易出错的惊人方法。 I also made a couple of changes to it to support mutlibindings but that's offtopic, you get the basic idea from the code: get the data context from the row and the CurrentCell property of the grid and compare if they are the same.我还对其进行了一些更改以支持多绑定,但这是题外话,您可以从代码中获得基本思想:从行中获取数据上下文和网格的 CurrentCell 属性,并比较它们是否相同。

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{amazing:ExpressionConverter 'x[0] == x[1]'}">
                        <Binding RelativeSource="{RelativeSource Self}" Path="DataContext"></Binding>
                        <Binding ElementName="Grid" Path="CurrentCell" Converter="{amazing:ExpressionConverter 'x.Item'}"></Binding>
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="Red"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

I found a great solution for this on MSDN.我在 MSDN 上找到了一个很好的解决方案。 Linking to my answer on a similar question:链接到我对类似问题的回答:

Highlight row when SelectionUnit is set to Cell当 SelectionUnit 设置为 Cell 时突出显示行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF DataGrid中突出显示行并强制行/单元格刷新? - How to highlight a row and force row/cell refresh in a WPF DataGrid? 单击单个单元格时,如何突出显示整个DataGrid行? - How can I highlight an entire DataGrid row when a single cell is clicked? 如何将所选 WPF DataGrid 单元格的值设置为上面单元格的值? - How do I set the value of the selected WPF DataGrid cell to the value of the cell above? 如何为DataGrid WPF的单个单元格而不是单行设置背景? - How to set background for single cell of datagrid wpf not single row? 在WPF数据网格中,如何在单元格编辑后获得新行? - In a WPF datagrid, how do I get the new row after cell edit? 当我在单击时禁用行选择时,仅选择复选框时如何突出显示 DataGrid 行? - How do I highlight DataGrid row when only selecting checkbox when I've disabled row selection on clicking? WPF Datagrid高亮显示行和列 - WPF Datagrid highlight row and column WPF DataGrid高亮显示整个行 - WPF DataGrid highlight entire row 以编程方式添加DataTrigger时,如何继承WPF DataGrid单元格样式? - How do I Inherit a WPF DataGrid Cell Style when programmatically adding a DataTrigger? 当数据网格获得焦点时,如何关闭WPF XCeed DataGridControl并始终选择第一个单元格? - How do I turn off the WPF XCeed DataGridControl alway selecting the first cell when the datagrid gets focus?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM