将命令从ViewModel绑定到UserControl中的事件 [英] Binding a command from ViewModel to an event within a UserControl

查看:64
本文介绍了将命令从ViewModel绑定到UserControl中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MainWindowViewModel,它包含一些命令。我还有一个带有滑块的UserControl,该滑块显示在MainWindow.xaml中。当我在UserControl中移动滑块时,我想要调用MainWindowViewModel中的命令。

I have MainWindowViewModel and it contains some commands. I also have a UserControl with a slider that is displayed in MainWindow.xaml. When I move the slider within the UserControl, I want a command from MainWindowViewModel to be called.

最好的方法是什么?

我觉得应该有办法让我从滑块中公开ValueChanged事件(比如依赖属性),然后绑定我的命令那个暴露的事件。

I feel like there should be a way for me to expose the ValueChanged event from the slider (like a dependency property) and then bind my command to that exposed event.

这样的事情......

Something like this...

< controls:MyUserControl SliderValueChanged = {Binding Path = MyCommand} />

<controls:MyUserControl SliderValueChanged={Binding Path=MyCommand} />

谢谢!

推荐答案

将滑块的value属性绑定到viewmodel中的double propfull。

Bind the value property of the slider to a double propfull in your viewmodel.

调用你想要的任何方法从propfull的setter开始运行。

Call whatever method you want to run from the setter of the propfull.


这篇关于将命令从ViewModel绑定到UserControl中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆