Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在MVVM中更新滑块值_Wpf_Mvvm - Fatal编程技术网

Wpf 在MVVM中更新滑块值

Wpf 在MVVM中更新滑块值,wpf,mvvm,Wpf,Mvvm,最初,我在代码隐藏中有一个按钮事件 private async void SomeCommand(object sender, RoutedEventArgs e) { MySlider.Value = MySlider.Value - 1; 在我的XAML中 <Button Command="{Binding SomeCommand}">Do Something</Button> 我不知道方法DoSomething中的代码是什么?如果要使用MVVM方法,应

最初,我在代码隐藏中有一个按钮事件

private async void SomeCommand(object sender, RoutedEventArgs e)
{
    MySlider.Value = MySlider.Value - 1;
在我的XAML中

 <Button Command="{Binding SomeCommand}">Do Something</Button>

我不知道方法
DoSomething
中的代码是什么?

如果要使用MVVM方法,应该使用可以绑定的属性
值。
您的xaml看起来像

<Slider x:Name="MySlider" Value="{Binding Value}" />
<Button Command="{Binding SomeCommand}">Do Something</Button>
public class MyViewModel : ViewModel
{
    private readonly ICommand someCommand;
    public MyViewModel()
    {
        this.someCommand = new DelegateCommand(this.DoSomething, this.CanDoSomething);
    }

    public ICommand SomeCommand
    {
        get { return this.someCommand; }
    }

    private void DoSomething(object state)
    {
        Value = Value - 1;
    }

    private bool CanDoSomething(object state)
    {
       //something like boundaries check of slider values and async is running and so on
    }

    private int _value;
    public int Value
    {
        get { return _value; }
        set 
        {
             if (_value != value)
             {
                 _value = value;
                 OnPropertyChanged(); //raise your propertyChanged event handler.
             }
        }
    }
}
public class MyViewModel : ViewModel
{
    private readonly ICommand someCommand;
    public MyViewModel()
    {
        this.someCommand = new DelegateCommand(this.DoSomething, this.CanDoSomething);
    }

    public ICommand SomeCommand
    {
        get { return this.someCommand; }
    }

    private void DoSomething(object state)
    {
        Value = Value - 1;
    }

    private bool CanDoSomething(object state)
    {
       //something like boundaries check of slider values and async is running and so on
    }

    private int _value;
    public int Value
    {
        get { return _value; }
        set 
        {
             if (_value != value)
             {
                 _value = value;
                 OnPropertyChanged(); //raise your propertyChanged event handler.
             }
        }
    }
}