MVVM 在视图中调用方法(通过 ViewModel 从 ControlTemplate)
我想知道如何在不使用事件聚合的情况下解决以下问题。这是针对 WPF 3.5 SP1 的,因此 CallMethodBehavior 不可用。
简单场景:单击 ControlTemplate 内的按钮需要触发 VM。我使用了 CaliburnMicro 的 ActionMessage,效果很好。在 ViewModel 内部,我想触发 View 内部的一个方法,该方法仅启动自定义转换(没有真正的逻辑)。我尝试了很多事情,但没有成功。
我在我的视图中创建了一个属性,它可以调用该方法,但我无法使用触发器为该属性设置新值,因为我无法告诉设置器将控件模板外部的属性作为目标。
所以本质上我想更新视图模型中的属性并触发视图类中的设置属性。或者,如果您有任何想法如何解决这个问题:我愿意接受新想法! :D
问候 戈佩
I would like to know how the following problem can be solved WITHOUT using Event Aggregation. This is for WPF 3.5 SP1, so the CallMethodBehavior is not available.
Simple Scenario: A click on a button inside a ControlTemplate needs to be triggered to the VM. I used CaliburnMicro's ActionMessage which worked fine. Inside the ViewModel I want to trigger a method inside the View, which only starts a custom transition (no real logic). I tried many things, but I did not work out.
I created a Property in my view, which could call the method but I am not able to use Triggers to set a new value for the property, because I can't tell the setter to target a property outside the controltemplate.
So in essence I want to update a Property in the viewmodel and trigger a set-property in the view class. Or if you have any idea how to get around this at all: I am open to new ideas! :D
Regards
Gope
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最简单的方法是从您的虚拟机公开一个事件并在您看来订阅它?
我使用 this 来发送 DialogResult 的对话框来自虚拟机
i think the most simple way is to expose an event from your vm and subscribe to it in your view?
i used this for dialogs to send DialogResult from vm
我找到了一个可以接受的解决方案:我将 CallMethodAction 移植到 3.5 并编写了自己的 PropertyChangedTrigger。通过视图模型中的 PropertyChange 调用视图内的方法非常简单 - 孩子们:不要在家里尝试这个。仅适用于特殊场景! :D
在下面找到我的代码:
用法:
PropertyChangedTrigger:
CallMethodAction:
希望这对任何人都有帮助。欢迎提出所有问题! 记住:所有这些都可以在 Expression Blend SDK 4 中找到。此代码仅适用于被迫使用 3.5 等旧版本的人
请
戈佩
戈佩
I found a solution I can live with: I ported the CallMethodAction to 3.5 and wrote my own PropertyChangedTrigger. It's pretty simple to call a method inside the view via a PropertyChange in the viewmodel - Kids: don't try this at home. It's only for special scenarios! :D
Find my code below:
usage:
PropertyChangedTrigger:
CallMethodAction:
Hope this helps anybode. All questions are welcome! Remeber: all this can be found in the Expression Blend SDK 4. This code is only for people who are forced to work with older versions like 3.5
Regards
Gope
Gope