具有ListView的WPF MVVM ICommnad [英] WPF MVVM ICommnad with ListView
问题描述
你好,
我对WPF中的ICommand和MVVM有疑问.
我已经看到处理事件的最常见方法是使用ICommand接口.
我了解如果我想向按钮添加命令,则可以执行以下操作:
<Button Command="{MyCommand}" />
但是我不明白如何创建一个命令来进行selectionchanged或ListView的操作?
有没有人知道任何好的示例,或者可以通过一些简单的代码帮助我?
-------------------------------------------------- -------------------------------------------------
您好,
在listView上,我对SelectionChanged的Icommand实现仍然有问题.
我找到了这篇文章: http://blog.fossmo.net/post/How-to-create-an-attached-property-in-WPF-using-a-ComboBox.aspx [ ^ ]但我"我不确定他的意思是:"MyCommand可以是来自CAG的DelegateCommand."
Hello,
I have a question about ICommand and MVVM in WPF.
I have seen that the most common way to handle events is by using interface ICommand.
I understand that if I want to add a command to a button, I do something like this:
<Button Command="{MyCommand}" />
But I don''t understand how to create a command to selectionchanged or something for a ListView?
Does anyone know any good examples out there, or could help me with some simple code?
---------------------------------------------------------------------------------------------------
HI Again,
I have still problems with Icommand implementation of SelectionChanged at listView.
I found this post: http://blog.fossmo.net/post/How-to-create-an-attached-property-in-WPF-using-a-ComboBox.aspx[^] but I''m not sure what he meean by: "MyCommand can be a DelegateCommand from CAG." How could a CAG class look like?
推荐答案
首先,ICommand不是支持事件的机制.它是您应该在WPF或Silverlight中执行基于命令的功能的机制.我之所以这么说是因为它超出了事件应做的事情-使用命令性基础结构,您还可以通过确定是否可以发生某些事情来影响UI,因为ICommand接口还支持CanExecute方法.当某些东西支持ICommand时,UI实际上将考虑此方法,以便当CanExecute返回false时您不能按下按钮.
如您所知,您无法将ICommand绑定到更改的选择,因为它不支持命令绑定.起初,这似乎是一个真正的问题,但是有许多解决方案都支持这一点.最好的实现可能是MVVM Light中的EventToCommand支持,您可以在此处 [此处演示了此行为a> [ ^ ].
First of all, ICommand is not the mechanism to support events. It is the mechanism by which you should perform command based functionality in WPF or Silverlight. The reason I say this is because it goes beyond what an event should do - with commanding infrastructure, you can also affect the UI by determining whether or not something can happen because the ICommand interface also supports a CanExecute method. When something supports ICommand, the UI will actually take this method into account, so that you can''t press a button when CanExecute returns false.
As you are now aware, you can''t bind an ICommand to a selection changed because it doesn''t support command binding. At first, this would seem to be a real problem, but there are a number of solutions that support this. Probably the best implementation is the EventToCommand support in MVVM Light which you can find here[^].
Laurent demonstrates using this behavior here[^].
我遇到的最好的示例是附加属性.可以在Claus Conrads博客 WPF ListView MVVM和缺少ICommand [ ^ ].附加属性可用于各种事物,绝对值得一试.
希望对您有帮助
The best examples I have come across use Attached Properties. A good example of this can be found on Claus Conrads Blog WPF ListView MVVM and missing ICommand[^]. Attached properties can be used for all sorts of things and are definitely worth getting comfortable with.
Hope this helps
这篇关于具有ListView的WPF MVVM ICommnad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!