【问题标题】:Highlighting the selected Item in WPF Datagrid突出显示 WPF Datagrid 中的选定项
【发布时间】:2019-02-14 14:10:05
【问题描述】:

我的问题似乎是一件容易的事,但不知何故,我尝试的所有事情都不会给我想要的结果。

在我的 MainWindow 中,我有一个 ContentControl,它绑定到一个名为“CurrentView”的变量,它是一个 ViewModel。我通过导航栏切换 CurrentView。 在我的第一个视图中,有一个 DataGrid。当我单击该 DataGrid 上的元素时,行以蓝色突出显示,所选项目保存在我的 ViewModel 中。

当我现在跳转到一个新视图并返回到第一个视图时,在 DataGrid 中仍然选择了选定的项,但没有突出显示行…

我尝试了很多事情,但不知何故我无法突出显示行。

我有什么遗漏吗?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您需要聚焦DataGrid 以使所选行默认突出显示。例如,您可以在要导航到的视图的 Loaded 事件处理程序中调用 DataGridFocus() 方法:

    public partial class View1 : UserControl
    {
        public View1()
        {
            InitializeComponent();
            Loaded += (s, e) => dataGrid1.Focus();
        }
    }
    

    【讨论】:

    • 我试过你的想法。与 FocusManager.FocusedElement="{Binding ElementName=ArtikelGrid}" 的方法相同吗?这不起作用。
    • 好吧,我想你帮了我。我注意到行被突出显示,但是由于列是在后面的代码中动态生成的,因此单元格不会被突出显示,因为不知何故,它们是在行获得焦点后生成的......我在某些事情上
    • 请阅读this blog post。它解释了您需要了解的有关在 DataGrid 中突出显示单元格的所有信息。
    • 感谢您的链接。我以前读过那篇文章。我想我找到了我的问题。我以前没有想到,但是在 DataGrid 选择它的项目的那一刻,Datagrid 没有任何列。这就是为什么没有具有焦点的细胞的原因。我认为有了这些知识,我现在可以解决我的问题了。
    • 我得到了一个有效的解决方案,但只有当我从代码中更改所选项目时。当我从代码中选择的项目已经是选定的项目时,它不起作用。
    【解决方案2】:

    AFAIK 您需要专注于特定行。 我使用这种行为:

    使用 System.Windows; 使用 System.Windows.Controls; 使用 System.Windows.Input; 使用 System.Windows.Interactivity;

    namespace blaa
    {
    class DataGridRowBehavior : Behavior<DataGridRow>
    {
        public static bool GetIsDataGridRowFocussedWhenSelected(DataGridRow dataGridRow)
        {
            return (bool)dataGridRow.GetValue(IsDataGridRowFocussedWhenSelectedProperty);
        }
    
        public static void SetIsDataGridRowFocussedWhenSelected(
          DataGridRow dataGridRow, bool value)
        {
            dataGridRow.SetValue(IsDataGridRowFocussedWhenSelectedProperty, value);
        }
    
        public static readonly DependencyProperty IsDataGridRowFocussedWhenSelectedProperty =
            DependencyProperty.RegisterAttached(
            "IsDataGridRowFocussedWhenSelected",
            typeof(bool),
            typeof(DataGridRowBehavior),
            new UIPropertyMetadata(false, OnIsDataGridRowFocussedWhenSelectedChanged));
    
        static void OnIsDataGridRowFocussedWhenSelectedChanged(
          DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            DataGridRow item = depObj as DataGridRow;
            if (item == null)
                return;
    
            if (e.NewValue is bool == false)
                return;
    
            if ((bool)e.NewValue)
                item.Selected += OndataGridRowSelected;
            else
                item.Selected -= OndataGridRowSelected;
        }
        static void OndataGridRowSelected(object sender, RoutedEventArgs e)
        {
            DataGridRow row = e.OriginalSource as DataGridRow;
            // If focus is already on a cell then don't focus back out of it
            if (!(Keyboard.FocusedElement is DataGridCell) && row != null)
            {
                row.Focusable = true;
                Keyboard.Focus(row);
            }
        }
    
      }
    }
    

    用法:

            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="local:DataGridRowBehavior.IsDataGridRowFocussedWhenSelected" Value="true"/>
                </Style>
            </DataGrid.RowStyle>
    

    【讨论】:

    • 到目前为止,我从未处理过行为。我正在寻找一个小修复
    猜你喜欢
    • 2013-06-11
    • 2015-10-31
    • 2013-12-20
    • 2011-07-09
    • 1970-01-01
    • 2011-10-01
    • 2023-03-29
    • 2015-01-18
    • 2011-05-27
    相关资源
    最近更新 更多