如何在视图模型中处理Slider.ValueChanged事件? [英] How to handle the Slider.ValueChanged event in a view model?

查看:71
本文介绍了如何在视图模型中处理Slider.ValueChanged事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PlayerV.xaml 视图,其中有一个滑块:

I have a PlayerV.xaml View with a Slider inside:

<Slider Value="{Binding CurrentProgress}"/>

并有一个按钮:

<Button Content="next song" Command="{Binding playNext}"/>

按钮正常工作.Button的 playNext 命令包含在 PlayerVM.cs

Button works correct. Button's playNext command is contained in PlayerVM.cs

我希望滑块的ValueChanged调用存储在 PlayerVM.cs 中的函数:

I want slider's ValueChanged to call a function which is stored in PlayerVM.cs:

[1]:< Slider Value ="{Binding CurrentProgress}" ValueChanged ="{Binding playNext}"/>

我知道[1]的语法不正确,为了便于说明我使用了它.

I know [1] has an incorrect syntax, I used it for sake of clarity of explanation.

====附加说明====

====Additional Explanation====

我知道我会写:

<Slider ValueChanged="Slider_ValueChanged" Value="{Binding CurrentProgress}" />

PlayerV.xaml.cs 中,

And in PlayerV.xaml.cs there will be

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        //some logic (actions) 
    }    
}

但是我不希望有任何逻辑.我希望它位于 PlayerVM.cs 中(就像按钮的命令处理程序函数一样).怎么做?同样在我的App.xaml.cs启动函数中是:

But I don't want any logic in there. I want it to be in PlayerVM.cs (like button's command handler functions). How to do that? Also in my App.xaml.cs startup function is:

private void OnStartup(object sender, StartupEventArgs e)
{
    MainWindow _mainWindow = new MainWindow();
    PlayerVM playerVM = new PlayerVM();
    _mainWindow.DataContext = playerVM;
    _mainWindow.Show();
 }

推荐答案

您有两个选择.首先,尽管您说过不想在后面使用代码,但是一个解决方案是让您做到这一点.在 ValueChanged 事件处理程序中,只要更改值,就可以简单地调用视图模型方法:

You have two options. First, despite what you said about not wanting to use code behind, one solution is for you to do just that. In the ValueChanged event handler, you can simply call your view model method when ever the value is changed:

private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    Slider slider = sender as Slider;
    PlayerVM viewModel = (PlayerVM)DataContext;
    viewModel.YourMethod(slider.Value);
} 

我之所以提供此解决方案,是因为我怀疑您是MVVM的新手,但仍然认为您不被允许使用后面的代码.实际上,根本不是这种情况,对于纯粹的UI问题,这是一个好地方.

I've offered this solution because I suspect that you're new to MVVM and still think that you're not allowed to use the code behind. In fact, that is not the case at all and for purely UI matters such as this, it's a good place for it.

另一种选择是将数据属性直接绑定到 Slider.Value .随着 Slider 值的更改,属性也会更改.因此,您可以简单地从数据绑定属性设置器中调用您的方法:

Another option is just to data bind a property directly to the Slider.Value. As the Slider value is changed, so will the property be. Therefore, you can simply call your method from the data bound property setter:

public double CurrentProgress
{
    get { return currentProgress; }
    set
    {
        currentProgress = value;
        NotifyPropertyChanged("CurrentProgress");
        YourMethod(value);
    }
}

另一种选择涉及在自定义附加属性中处理 ValueChanged 事件.该解决方案比其他解决方案要多得多,因此我希望将您引向我已经为其他问题写的一些答案,而不是将其全部重新编写.请查看我对将焦点设置为使用MVVM的WPF控件?

One further option involves handling the ValueChanged event in a custom Attached Property. There is a bit more to this solution than the others, so I'd prefer to direct you to some answers that I have already written for other questions, rather than re-writing it all again. Please see my answers to the How to set Focus to a WPF Control using MVVM? and WPF C# - navigate WebBrowser on mouseclick through Binding questions for explanations and code examples of this method.

这篇关于如何在视图模型中处理Slider.ValueChanged事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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