从 Silverlight ItemsControl 删除项目的模式
我有一个包含 ItemsControl 的 Silverlight 页面。它看起来像这样
-- Name Description [Add]
-- Thing1 The first thing [Edit] [Delete]
-- Thing2 The second thing [Edit] [Delete]
,其中 [Edit]
、[Delete]
和 [Add]
是按钮。
目前,我正在将控件绑定到 Thing
集合,并使用模板来显示属性,并绑定到 ViewModel 中的 Edit
命令。
对于 ThingViewModel
来说,拥有导致其删除自身的 Delete
命令是没有意义的(对我来说);
- 看起来不干净
- 该物体不知道它在集合中,因此无法将其自身从集合中删除
那么连接[Delete]
按钮的最佳模式是什么?
I have a Silverlight page that contains an ItemsControl. It looks something like this
-- Name Description [Add]
-- Thing1 The first thing [Edit] [Delete]
-- Thing2 The second thing [Edit] [Delete]
where [Edit]
, [Delete]
, and [Add]
are buttons.
Currently I'm binding the control to a collection of Thing
and using a template to display the properties, and bind to an Edit
Command in my ViewModel.
It doesn't make sense (to me) for the ThingViewModel
to have a Delete
Command which causes it to delete itself;
- doesn't seem clean
- the Thing doesn't know that it is in a collection so can't remove itself from the collection
So what's the best pattern to wire up the [Delete]
button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“删除”代码不会在集合中单个项目的 ViewModel 上运行,而是会(以某种方式)冒泡到包含集合的 ViewModel,并在那里进行处理。
伪代码:
冒泡事件的一种方法是让 ItemViewModel 知道它的父 ViewModel
使用 MVVM 框架(如 Caliburn.Micro 或 MVVM-Lite)有更好的方法,但这至少让您开始了解如何考虑这些类型ViewModel 中的操作。
本质上 - 您的 ViewModel 应该能够执行所有用户操作,而不需要任何类型的视图。 (您应该能够运行一些测试代码,并且让您的虚拟机按预期工作,而无需绑定视图)
The "Delete" code will not be run on the ViewModel for the individual item, in the collection, rather it would be bubbled up (somehow) to the ViewModel that contains the collection, and processed there.
Pseudocode:
One way to bubble up the event is to have the ItemViewModel aware of it's parent ViewModel
There are better ways with a MVVM framework like Caliburn.Micro or MVVM-Lite, but this at least gets you started on how to think about these kinds of operations in your ViewModel.
Essentialy - your ViewModel should be able to do all of your user operations without requiring a View of any kind. (You should be able to run some test code, and have your VM work as intended without a bound View)
这是我想出的——以一种非常简化的形式。当
RelativeSource
不可用时,这是一个显而易见的选择。ElementName
属性这适用于
ItemsControl
但我无法获得此模式还适用于 DataGrid。Here's what I've come up with - in a very simplified form. It's an obvious choice when
RelativeSource
is not available.ElementName
property on the binding for the child / itemThis works for an
ItemsControl
but I haven't been able to get this pattern to work for aDataGrid
yet.如果您使用列表框,以便用户可以选择一行,然后将命令放入包含该集合的虚拟机中,您将获得更清晰的实现。
这是带有删除按钮的行的 xaml。诀窍是使用“RelativeSource”
和ViewModel代码
将按钮绑定到父项DeleteCommand ,哦,我刚刚看到上面关于silverlight和relativeSource的评论。我用 WPF 做了这个并测试了它,它有效,它可能不适用于 silverlight
If you use a listbox so that the user can select a row and then put your commands in the VM that contains the collection, you get a cleaner implementation.
here is the xaml for a row with a delete button. The trick is to bind the button to the parent items DeleteCommand by using "RelativeSource"
And the ViewModel code
oh, i just saw the above comment about silverlight and relativeSource. I did this with WPF and tested it out, which worked, it may not work for silverlight