PRISM 代理命令 +样本数据
我有一个 ViewModel,它使用 DelegateCommand
属性绑定到 Button
的 Command
属性。
问题是我的示例数据不喜欢 DelegateCommand
对象。它抱怨:类型“DelegateCommand”不包含任何可访问的构造函数。
此外,唯一公开的属性是IsActive
属性。
<local:MyViewModel xmlns:local="clr-namespace:MyNamespace"
xmlns:prism="http://www.codeplex.com/prism">
<local:MyViewModel.Age>47</local:MyViewModel.Age>
<local:MyViewModel.PurchaseAlcohalCommand>
<prism:DelegateCommand IsActive="True" />
</local:MyViewModel.PurchaseAlcohalCommand>
</local:MyViewModel>
I have a ViewModel that uses a DelegateCommand
property to bind to a Button
's Command
property.
The problem is my sample data does not like the DelegateCommand
object. It complains that: The type "DelegateCommand" does not include any accessible constructors.
Also, the only exposed property is the IsActive
property.
<local:MyViewModel xmlns:local="clr-namespace:MyNamespace"
xmlns:prism="http://www.codeplex.com/prism">
<local:MyViewModel.Age>47</local:MyViewModel.Age>
<local:MyViewModel.PurchaseAlcohalCommand>
<prism:DelegateCommand IsActive="True" />
</local:MyViewModel.PurchaseAlcohalCommand>
</local:MyViewModel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改视图模型以公开 ICommand 而不是 DelegateCommand。 DelegateCommand只是ICommand的一个实现;如果您稍后想从 MvvmLight 切换到 RelayCommand,则不必关心您的视图和示例数据。
我不确定这是否能解决您的问题,但我怀疑可能会。另外,这只是一个很好的编程实践。
Change your view model to expose an ICommand instead of a DelegateCommand. DelegateCommand is just an implementation of ICommand; if you later want to switch to RelayCommand from MvvmLight your view and your sample data should not have to care.
I'm not sure that this will solve your problem, but I suspect it might. Plus it's just a good programming practice.
根据
DelegateCommand
的设置方式,它在激活时不会执行任何操作。如果这是所需的行为,我的建议是干脆不声明它。 WPF 将优雅地处理绑定到null
ICommand
对象的情况。或者,如果您需要将其绑定到实例化的 DelegateCommand,则可以对
DelegateCommand
进行子类化以包含无参数构造函数。如果您希望它绑定到
DelegateCommand
并且希望该DelegateCommand
在命令被触发时实际做某些事情,那就有点麻烦了更复杂。您必须使用我之前提到的子类DelegateCommand
,但您还必须能够在 XAML 中定义委托。我认为那里有一些示例,但我猜它们涉及标记扩展之类的东西以及类似性质的东西。这种方法的投资回报可能会有点低,但你的里程可能会有所不同。最后一种替代方法是通常的处理方式:在 ViewModel 的构造函数中定义
DelegateCommands
。The way your
DelegateCommand
is setup, it won't do anything when it is activated. If that is the desired behavior, my suggestion would be to simply not declare it. WPF will gracefully handle being bound to anull
ICommand
object.Alternatively, if you need it to bind to an instantiated DelegateCommand, you could subclass
DelegateCommand
to include a parameterless constructor.If you wanted it to bind to a
DelegateCommand
and you wanted thatDelegateCommand
to actually DO something when the command is triggered, that would get a bit more complicated. You would have to use the subclassedDelegateCommand
I mentioned before, but you would also have to be able to define a delegate in XAML. I think there are samples out there, but I would guess they involve things like markup extensions and things of those nature. Your return on investment in this approach may be a little low, but your mileage may vary.One last alternative that is the way this is typically handled: define your
DelegateCommands
in the constructor of your ViewModel.