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.
}
}
}
}