【问题标题】:WPF DataGrid programmatic multiple row selectionWPF DataGrid 编程多行选择
【发布时间】:2011-01-30 14:30:38
【问题描述】:

还有什么比下面的示例更简单的吗?我确实有绑定到 DataGrid lstLinks 的可观察集合(代码中的“列表”)

for (int i = 0; i < list.Count ; i++)
{
    object rowItem = lstLinks.Items[i] ; 
    DataGridRow visualItem =  (DataGridRow)lstLinks.ItemContainerGenerator.ContainerFromItem(rowItem);
    if ( visualItem == null ) break;  
    if (list[i].Changed)
        visualItem.IsSelected = false;
    else
         visualItem.IsSelected = false; 

}

【问题讨论】:

  • 而且它只适用于可见行。

标签: wpf select datagrid wpfdatagrid


【解决方案1】:

Salam MicMit :)

是的,有一个更简单的解决方案,您只需将您想要的项目从绑定列表添加到您的 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"},
                    new Employee {Code = "E431", Name = "Zuhair Zein"},
                    new Employee {Code = "E431", Name = "Layla Abdullah"},
                };

        /* 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, add it to your DataGrid SelectedItems collection to
         * add the item to the selected rows
         */
        if (emp != null)
            empDataGrid.SelectedItems.Add(emp);
    }
}

【讨论】:

  • 这打破了 MVVM 模式和 MVC,但这并没有在问题中说明。
  • @TomerW。是的,但这是另一个问题。我更愿意专注于问题并尽可能简化解决方案。
  • 可能......哎呀......如果你大约 2 个月前问过我,我会说 1. 我是一个 winforms 人...... 2. 完全一样 :)
【解决方案2】:

MVVM 需要更多招标解决方案,
但它是 MVVM 并且完全可测试且易于维护。

看这里 Managing multiple selections with MVVM

【讨论】:

    【解决方案3】:

    是的,这个解决方案比入侵 DataGrid 控件要好,如果你只想选择一行,也可以使用以下代码:

    myDataGrid.SelectedItem = item;
    

    其中项目是绑定 DataGrid 的项目之一。

    【讨论】:

    • 多选到底是怎么做到的?
    【解决方案4】:

    假设 lstLinks 是 WPF DataGrid,这会将位置 i 的项目添加到所选项目:

    lstLinks.SelectedItems.Add(lstLinks.Items[i]);
    

    取消选择所有内容:

    lstLinks.SelectedItems.Clear();
    

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 2011-08-12
      • 2011-11-01
      • 2012-03-12
      • 1970-01-01
      • 2012-02-03
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多