WPF Datagrid设置选定的行

Ton*_*ion 17 wpf select datagrid selecteditem

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

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

编辑:

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

ser*_*nko 36

请检查以下代码是否适合您; 它遍历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;
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这有帮助,问候

  • 在某些情况下,您可能会发现在 `dataGrid.ScrollIntoView()` 之前添加 `dataGrid.UpdateLayout()` 非常有用 - 如 [MSDN](http://msdn.microsoft.com/en-us/library/) 中所述windows/apps/hh968031.aspx): *当 ItemsSource 集合的内容发生变化时,尤其是在集合中添加或删除了许多项时,您可能需要在调用 ScrollIntoView 之前调用 UpdateLayout 以使指定项滚动到视口。* (2认同)

Moh*_*dil 22

您无需遍历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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 8

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

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

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

最好的祝福

马尔钦


Rob*_*ney 7

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

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

DataView工具IEnumerable<DataRowView>(更多或更少),并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<DataRowView>()
                    .Where(x => x.Row.Field<string>("text").Contains(_SearchText))
                    .FirstOrDefault();
            }
        }
    }

    public DataTable Table { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

这是使用它的XAML:

<Window x:Class="DataGridSelectionDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
        xmlns:DataGridSelectionDemo="clr-namespace:DataGridSelectionDemo" 
        Title="DataGrid selection demo" 
        Height="350" 
        Width="525">
    <Window.DataContext>
        <DataGridSelectionDemo:DataTableWrapper />
    </Window.DataContext>
    <DockPanel>
        <Grid DockPanel.Dock="Top">
        <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label>Text</Label>
            <TextBox Grid.Column="1" 
                     Text="{Binding SearchText, Mode=TwoWay}" />
        </Grid>
        <dg:DataGrid DockPanel.Dock="Top"
                     ItemsSource="{Binding Table}"
                     SelectedItem="{Binding Row, Mode=TwoWay}" />
    </DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

  • 尽管其他人似乎已经获得了更多的赞成,但这似乎是讨论如何以最佳方式提供工具功能的方式将数据集绑定到XML控件的方式. (2认同)