Wpf工具包datagrid DataGridCheckBoxColumn on_click事件:

发布于 2024-08-28 12:39:15 字数 95 浏览 1 评论 0原文

我想获取选定的行,但 selecteditems 只有一行。我想要获得所有已检查的项目。我想我需要在选中复选框时添加事件处理程序,然后收集它们。我该如何以最好的方式做到这一点?

I want to get the selected rows, but selecteditems has only one row. I want get the all the checked item. I think I need to add event handler when the checkbox is clecked and then collect them all. How do I do this in a best way?

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

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

发布评论

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

评论(1

£冰雨忧蓝° 2024-09-04 12:39:15

您是否使用数据绑定来填充 DataGrid?如果是这样,将列的检查值绑定到支持对象中的 bool 可能是执行此操作的“最佳”(如:最佳实践)方法。这是一些示例代码:

<Window x:Class="CheckGridSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <tk:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
            <tk:DataGrid.Columns>
                <tk:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <tk:DataGridCheckBoxColumn Header="Selected" Binding="{Binding IsChecked}" />
            </tk:DataGrid.Columns>
        </tk:DataGrid>
        <Button Content="Which Items Are Checked?" Click="Button_Click" />
    </StackPanel>
</Window>

using System;
using System.Linq;
using System.Text;
using System.Windows;

namespace CheckGridSample
{
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = new[]
                {
                    new MyModel {Name = "Able"},
                    new MyModel {Name = "Baker", IsChecked = true},
                    new MyModel {Name = "Charlie"}
                };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var builder = new StringBuilder();
            foreach (var item in ((MyModel[]) DataContext).Where(m => m.IsChecked))
                builder.Append(Environment.NewLine + item.Name);
            MessageBox.Show("Checked:" + builder);
        }
    }

    public class MyModel
    {
        public string Name { get; set; }
        public bool IsChecked { get; set; }
    }
}

Are you using databinding to populate your DataGrid? If so, binding the checked value of your column to a bool in your backing object is probably the "best" (as in: best-practices) way to do this. Here is some example code:

<Window x:Class="CheckGridSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <tk:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
            <tk:DataGrid.Columns>
                <tk:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <tk:DataGridCheckBoxColumn Header="Selected" Binding="{Binding IsChecked}" />
            </tk:DataGrid.Columns>
        </tk:DataGrid>
        <Button Content="Which Items Are Checked?" Click="Button_Click" />
    </StackPanel>
</Window>

using System;
using System.Linq;
using System.Text;
using System.Windows;

namespace CheckGridSample
{
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = new[]
                {
                    new MyModel {Name = "Able"},
                    new MyModel {Name = "Baker", IsChecked = true},
                    new MyModel {Name = "Charlie"}
                };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var builder = new StringBuilder();
            foreach (var item in ((MyModel[]) DataContext).Where(m => m.IsChecked))
                builder.Append(Environment.NewLine + item.Name);
            MessageBox.Show("Checked:" + builder);
        }
    }

    public class MyModel
    {
        public string Name { get; set; }
        public bool IsChecked { get; set; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文