如何添加“选择”带 MVVM 的 Silverlight DataGrid 列

发布于 2024-11-26 10:52:19 字数 283 浏览 0 评论 0 原文

我需要在我的 DataGrid 中有一个带有复选框的列,以便用户选择各个行(稍后将执行操作)。网格 ItemsSource 绑定到 ObservableCollection。我认为最快的解决方案(不是最好的,但只是为了让它工作)是从集合中的类继承,添加一个我可以绑定到并从那里工作的 IsSelected 属性。但是,我开始使用的集合已经实例化,并且向下转型/逆变对我不起作用。而且,重新创建派生类的集合也是不可行的。

因此,我现在正在寻找一个简单的解决方案,可能在复选框列上有附加行为。我尽量不花太多时间在这上面,所以我需要一些想法。

I need to have a column with checkboxes in my DataGrid for the user to select various rows (that an operation would be performed on later). The grids ItemsSource is bound to an ObservableCollection. My quickest solution (not the best, but just to get it working) I thought would be to inherit from the class in the collection, adding in an IsSelected property I could bind to and just work from there. However, the collection I'm starting with is already instantiated, and downcasting/contravariance isn't working for me. And, it wouldn't be feasible to recreate a collection of the derived class.

So, I'm looking now for a simple solution with maybe an attached behavior on a checkbox column. I'm trying not to spend too much time on this, so I need some ideas.

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

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

发布评论

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

评论(2

眼泪都笑了 2024-12-03 10:52:19

我认为你正在做的事情是一个很好的方法。您使用的子类是原始类的“ViewModel”。

但是,我在 Laurent Bugnion 的这篇文章 使用了 DataGrid 控件的 SelectedItems 集合,不幸的是,您无法直接绑定到它,因为它不是 DependencyProperty,但他使用 EventToCommand 行为。 codeplex.com/" rel="nofollow">MVVMLight Toolkit 并将 SelectedItems 集合作为 CommandParameter 传递。

我希望这有帮助;)

I think what you're doing is a good way to do it. The subclass you're using is kind of a "ViewModel" for the original class.

However, there is another way that I found in this post by Laurent Bugnion that uses the SelectedItems collection of the DataGrid control, which you can't bind to directly, unfortunately, because it's not a DependencyProperty, but he uses the EventToCommand behavior from his MVVMLight Toolkit and passes the SelectedItems collection as a CommandParameter.

I hope this helps ;)

神经大条 2024-12-03 10:52:19

我最终不得不这样做:

1)在 TemplateColumn 中使用 CheckBox 而不是 DataGridCheckBoxColumn 因为 DataGridCheckBoxColumn 没有 Checked &要挂钩的未选中的事件。

2) 为复选框上的 Checked 和 Unchecked 事件创建一个行为类,每个行为类都有单独的命令;我必须创建一个行为类,因为虽然 CheckBox 已经具有 Command 属性,但似乎没有任何方法可以确定控件的选中/未选中状态,除非用作 CommandParameter,我需要为其传递信息 ;

3)从View传入对VM的引用,并在代码隐藏中将VM声明为静态资源 我必须这样做才能使用复选框绑定到虚拟机中的命令,同时可以访问相同的 Observab

I eventually had to do it this way:

1) Use a CheckBox inside a TemplateColumn instead of the DataGridCheckBoxColumn because the DataGridCheckBoxColumn doesn't have Checked & Unchechecked events to hook into.

2) Create a behavior class for the Checked and Unchecked events on the checkbox, with seperate commands for each; I had to create a behavior class because while the CheckBox has a Command property already, there didn't seem to be any way of determing the checked/unchecked status of the control, unless used as a CommandParameter, which I needed to pass information for the DataGrid bound item in.

3) Pass in a reference to the VM from the View, and declare the VM as a static resource in code behind; I had to do it this way in order to bind to the commands in my VM with the checkbox, while having access to the same Observab

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