首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >WPF数据网格设置选定行

WPF数据网格设置选定行
EN

Stack Overflow用户
提问于 2009-12-30 02:24:30
回答 8查看 121K关注 0票数 24

如何使用Datagrid.SelectedItem以编程方式选择行?

我是不是必须首先创建一个DataGridRow对象的IEnumerable并将匹配的行传给这个SelectedItem属性,或者我该怎么做呢?

编辑:

在选择行之前,我需要首先将第一个列单元格的单元格内容与TextBox.Text匹配。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2009-12-30 03:40:06

我的代码遍历datagrid第一列的单元格,检查单元格内容是否等于textbox.text值,然后选择该行。

for (int i = 0; i < dataGrid.Items.Count; i++)
{
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
    TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;
    if (cellContent != null && cellContent.Text.Equals(textBox1.Text))
    {
        object item = dataGrid.Items[i];
        dataGrid.SelectedItem = item;
        dataGrid.ScrollIntoView(item);
        row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        break;
    }
}
票数 42
EN

Stack Overflow用户

发布于 2011-03-30 16:07:15

您不需要遍历DataGrid行,您可以使用更简单的解决方案来实现您的目标。为了匹配您的行,您可以遍历绑定到DataGrid.ItemsSource属性的集合,然后以编程方式将该项分配给DataGrid.SelectedItem属性,或者,如果您希望允许用户选择多行,则可以将其添加到DataGrid.SelectedItems集合。请参见以下代码:

<Window x:Class="ProgGridSelection.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="OnWindowLoaded">
<StackPanel>
    <DataGrid Name="empDataGrid" ItemsSource="{Binding}" Height="200"/>
    <TextBox Name="empNameTextBox"/>
    <Button Content="Click" Click="OnSelectionButtonClick" />
</StackPanel>

public partial class MainWindow : Window
{
    public class Employee
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    private ObservableCollection<Employee> _empCollection;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnWindowLoaded(object sender, RoutedEventArgs e)
    {
        // Generate test data
        _empCollection =
            new ObservableCollection<Employee>
                {
                    new Employee {Code = "E001", Name = "Mohammed A. Fadil"},
                    new Employee {Code = "E013", Name = "Ahmed Yousif"},
                    new Employee {Code = "E431", Name = "Jasmin Kamal"},
                };

        /* Set the Window.DataContext, alternatively you can set your
         * DataGrid DataContext property to the employees collection.
         * on the other hand, you you have to bind your DataGrid
         * DataContext property to the DataContext (see the XAML code)
         */
        DataContext = _empCollection;
    }

    private void OnSelectionButtonClick(object sender, RoutedEventArgs e)
    {
        /* select the employee that his name matches the
         * name on the TextBox
         */
        var emp = (from i in _empCollection
                   where i.Name == empNameTextBox.Text.Trim()
                   select i).FirstOrDefault();

        /* Now, to set the selected item on the DataGrid you just need
         * assign the matched employee to your DataGrid SeletedItem
         * property, alternatively you can add it to your DataGrid
         * SelectedItems collection if you want to allow the user
         * to select more than one row, e.g.:
         *    empDataGrid.SelectedItems.Add(emp);
         */
        if (emp != null)
            empDataGrid.SelectedItem = emp;
    }
}
票数 25
EN

Stack Overflow用户

发布于 2011-06-08 08:17:55

我已经找到了类似问题的解决方案,也许我的方法会对你和任何面对它的人有所帮助。

我在XAML DataGrid定义中使用了SelectedValuePath="id",在编程上我唯一需要做的就是将DataGrid.SelectedValue设置为所需的值。

我知道这个解决方案有利有弊,但在特定情况下是快速和简单的。

诚挚的问候

Marcin

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1976087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档