当前位置:  开发笔记 > Android > 正文

WPF Datagrid设置选定的行

如何解决《WPFDatagrid设置选定的行》经验,为你挑选了4个好方法。

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

我首先要创建一个IEnumerableDataGridRow对象和匹配的行传递给此SelectedItem属性或我该怎么办呢?

编辑:

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



1> serge_gubenk..:

请检查以下代码是否适合您; 它遍历datagris第一列的单元格,并检查单元格内容是否等于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;
    }
}

希望这有帮助,问候



2> Mohammed A. ..:

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



    
    
    

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

    private ObservableCollection _empCollection;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnWindowLoaded(object sender, RoutedEventArgs e)
    {
        // Generate test data
        _empCollection =
            new ObservableCollection
                {
                    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;
    }
}



3> 小智..:

我搜索过类似问题的解决方案,也许我的方式会帮助你和任何面对它的人.

SelectedValuePath="id"在XAML DataGrid定义中使用,并且我只需要做的事就是设置DataGrid.SelectedValue为所需的值.

我知道这个解决方案有利有弊,但在具体情况下快速而简单.

最好的祝福

马尔钦



4> Robert Rossn..:

做你想做的事情比我更喜欢做的有点棘手,但那是因为你并没有直接将a绑定DataGrid到a DataTable.

当你绑定DataGrid.ItemsSource到a时DataTable,你真的将它绑定到默认值DataView,而不是绑定到表本身.这就是为什么,例如,DataGrid当您单击列标题时,您不必执行任何操作来创建排序行 - 该功能已被烘焙DataView,并且DataGrid知道如何访问它(通过IBindingList界面).

DataView工具IEnumerable(更多或更少),并DataGrid通过遍历该填充其项目.这意味着当你绑定DataGrid.ItemsSource到a时DataTable,它的SelectedItem属性将是a DataRowView,而不是a DataRow.

如果您了解所有这些,那么构建一个包装类非常简单,它允许您公开可以绑定的属性.有三个关键属性:

TableDataTable,

Row,类型的双向可绑定属性DataRowView,和

SearchText,一个字符串属性,当它被设置时,将DataRowView在表的默认视图中找到第一个匹配项,设置Row属性并引发PropertyChanged.

它看起来像这样:

public class DataTableWrapper : INotifyPropertyChanged
{
    private DataRowView _Row;

    private string _SearchText;

    public DataTableWrapper()
    {
        // using a parameterless constructor lets you create it directly in XAML
        DataTable t = new DataTable();
        t.Columns.Add("id", typeof (int));
        t.Columns.Add("text", typeof (string));

        // let's acquire some sample data
        t.Rows.Add(new object[] { 1, "Tower"});
        t.Rows.Add(new object[] { 2, "Luxor" });
        t.Rows.Add(new object[] { 3, "American" });
        t.Rows.Add(new object[] { 4, "Festival" });
        t.Rows.Add(new object[] { 5, "Worldwide" });
        t.Rows.Add(new object[] { 6, "Continental" });
        t.Rows.Add(new object[] { 7, "Imperial" });

        Table = t;

    }

    // you should have this defined as a code snippet if you work with WPF
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler h = PropertyChanged;
        if (h != null)
        {
            h(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // SelectedItem gets bound to this two-way
    public DataRowView Row
    {
        get { return _Row; }
        set
        {
            if (_Row != value)
            {
                _Row = value;
                OnPropertyChanged("Row");
            }
        }
    }

    // the search TextBox is bound two-way to this
    public string SearchText
    {
        get { return _SearchText; }
        set
        {
            if (_SearchText != value)
            {
                _SearchText = value;
                Row = Table.DefaultView.OfType()
                    .Where(x => x.Row.Field("text").Contains(_SearchText))
                    .FirstOrDefault();
            }
        }
    }

    public DataTable Table { get; private set; }
}

这是使用它的XAML:


    
        
    
    
        
        
                
                
            
            
            
        
        
    


尽管其他人似乎已经获得了更多的赞成,但这似乎是讨论如何以最佳方式提供工具功能的方式将数据集绑定到XML控件的方式.
推荐阅读
殉情放开那只小兔子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有