WPF DataGrid:如何以编程方式更改所选行?

发布于 2024-09-25 22:16:28 字数 96 浏览 3 评论 0原文

如何以编程方式更改选定的行?

我更改了所选项目和单元格,但我无法弄清楚如何突出显示整行。

注意:当用户使用鼠标或键盘选择一行时,突出显示效果很好。

How do I change the selected row programatically?

I change change the selected item and cell, but I cannot figure out how to get the whole row highlighted.

Note: The highlighting works fine when a user selects a row with mouse or keyboard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烈酒灼喉 2024-10-02 22:16:28

看看这个< /a> 页面。您需要 SelectionUnitSelectionMode 来指定如何在 DataGrid 中完成选择。

使用 SelectionUnit = FullRowSelectionMode = Single,用户一次只能选择一行。

编辑:尝试后,看起来 DataGrid.SelectedItem[i] 将选择整行。不幸的是,看起来您必须在为 DataGrid 的 SelectionChanged 属性创建的事件处理程序中手动设置突出显示。

take a look at this page. You need both the SelectionUnit and SelectionMode to specify how the selection is done in the DataGrid.

With SelectionUnit = FullRow and SelectionMode = Single, the user can only select one row at a time.

edit: after trying it out, it looks as though DataGrid.SelectedItem[i] will select an entire row. Unfortunately, it looks as if you will have to manually set the highlight in an event handler that you have to create for the SelectionChanged property of the DataGrid.

七度光 2024-10-02 22:16:28

似乎 SelectedItem 仅在包含元素(例如 UserControl)的 Loaded 事件之后被拾取。这似乎有效:

 public partial class UserControlClass
{
    public UserControlClass()
    {
        InitializeComponent();

        Loaded += UserControlClass_Loaded;
    }

    void UserControlClass_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        if (YourItemsControl.Items.Count > 0)
            YourItemsControl.SelectedItem = YourItemsControl.Items[0];
    }
}

如果 YourItemsControl 绑定到其中包含任何项目的集合,上面的代码将显示选定的第一个项目。

It seems that the SelectedItem only gets picked up after the Loaded event of the containg element (for example UserControl). This seems to work:

 public partial class UserControlClass
{
    public UserControlClass()
    {
        InitializeComponent();

        Loaded += UserControlClass_Loaded;
    }

    void UserControlClass_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        if (YourItemsControl.Items.Count > 0)
            YourItemsControl.SelectedItem = YourItemsControl.Items[0];
    }
}

The code above will show the first item selected if YourItemsControl is bound to a collection that has any items in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文