WPF DataGrid 编程式多行选择

发布于 2024-08-26 21:52:29 字数 433 浏览 7 评论 0 原文

还有比下面的示例更简单的吗?我确实有绑定到 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; 

}

Is there anything simpler than sample below ? I do have observable collection ( "list" in the code ) bound to 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; 

}

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

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

发布评论

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

评论(4

戏舞 2024-09-02 21:52:29

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);
    }
}

Salam MicMit :)

Yeah, there is a more simple solution, you need just to add the items you want from your bound list to your DataGrid SelectedItems collection, see the code below: [Don't forget to mark as answer if this post solved your problem :)]

<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);
    }
}
风流物 2024-09-02 21:52:29

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

看看这里
使用 MVVM 管理多个选择

MVVM Require more tender solution,
but it is MVVM and it is fully testable and easy to maintain.

Take a look here
Managing multiple selections with MVVM

风情万种。 2024-09-02 21:52:29

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

myDataGrid.SelectedItem = item;

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

Yeah, this solution is better than hacking into the DataGrid control, and if you want to select only one row you can also use the following code:

myDataGrid.SelectedItem = item;

where the item is one of the items bound the DataGrid.

韵柒 2024-09-02 21:52:29

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

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

要取消选择所有内容:

lstLinks.SelectedItems.Clear();

Assuming lstLinks is a WPF DataGrid, this adds item at position i to the selected items:

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

To unselect everything:

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