正确使用 MVVM 和命令
Proper using of MVVM and Commands
如何在 MVVM 中使用 ICommand
接口?
在视图中我添加了 DataContext
(这是我的 ViewModel : INotifyPropertyChanged
)并且我将控件的 属性 - IsInterminate
的进度条 - 绑定到 属性 的我的 ViewModel
。我的 ViewModel
的 属性 不是静态的,所以我需要 ViewModel
.
的实例
我的问题是:我应该如何在命令方法(此方法属于ViewModel
)中更新此ViewModel
实例(属性绑定到视图的进度条)?
<StatusBar Grid.Row="1">
<StatusBar.DataContext>
<viemodel:EventsViewModel x:Name="evm"/>
</StatusBar.DataContext>
<Label x:Name="lbStatusLabel" Width="70" Height="40" Content="{Binding EventsCollection.Count}"/>
<Separator />
<ProgressBar x:Name="pbProgress" Width="300" Height="40" IsIndeterminate="{Binding Pennding}"/>
</StatusBar>
class EventsViewModel : ViewModel
{
private static FWatch fw;
private static string fileName;
private static string pathToFile;
private static string pathToDirectory;
public EventsViewModel()
{
_startCommand = new RelayCommand(OpenFileCommand);
}
private ICommand _startCommand;
public ICommand StartCommand
{
get { return _startCommand; }
}
private static ObservableCollection<Event> _eventsCollection = new ObservableCollection<Event>();
public static ObservableCollection<Event> EventsCollection
{
get { return _eventsCollection; }
}
private static string _buttonContent = "Open file";
public string ButtonContent
{
get { return _buttonContent; }
set
{
_buttonContent = value;
NotifyPropertyChanged();
}
}
private bool _pending = false;
public bool Pennding
{
get { return _pending; }
set
{
_pending = value;
NotifyPropertyChanged();
}
}
private void OpenFileCommand()
{
// Here I want to update field _pennding - is it right? Or should I delegate it?
// Should I update `Pendding` property of `ViewModel` where is command's method or I should do it in behind-code of view?
}
}
您应该从命令处理程序设置您的 "Pending" 属性。在您的情况下,它是 OpenFileCommand。
private void OpenFileCommand()
{
this.Pending = true;
}
您必须使用一些命令基础结构组件。有几个可用。
例如 MVVM Light 就是一个不错的选择。
看看这里的一些提示:
How to use RelayCommand with the MVVM Light framework
但是您需要在表单上添加一个按钮,并将其绑定到命令以触发 ViewModel
上的操作。
Pending
应该在ViewModel
中进行操作。绑定负责其余的工作。
要引发 "property changed" 通知,我使用:
this.OnPropertyChanged(() => Breedte);
即改变的 属性 在最终引发的事件中被传递。我在你的代码中没有看到这一点。
你至少需要类似上面的东西或者
NotifyPropertyChanged("propertyName");
否则框架不知道改变了什么以及如何调整GUI。
好的,我知道了。
使用 xaml 中的数据上下文创建实例。我使用了 dataconytext 标签 3 次,我有 3 个 viemodel 实例。
如何在 MVVM 中使用 ICommand
接口?
在视图中我添加了 DataContext
(这是我的 ViewModel : INotifyPropertyChanged
)并且我将控件的 属性 - IsInterminate
的进度条 - 绑定到 属性 的我的 ViewModel
。我的 ViewModel
的 属性 不是静态的,所以我需要 ViewModel
.
我的问题是:我应该如何在命令方法(此方法属于ViewModel
)中更新此ViewModel
实例(属性绑定到视图的进度条)?
<StatusBar Grid.Row="1">
<StatusBar.DataContext>
<viemodel:EventsViewModel x:Name="evm"/>
</StatusBar.DataContext>
<Label x:Name="lbStatusLabel" Width="70" Height="40" Content="{Binding EventsCollection.Count}"/>
<Separator />
<ProgressBar x:Name="pbProgress" Width="300" Height="40" IsIndeterminate="{Binding Pennding}"/>
</StatusBar>
class EventsViewModel : ViewModel
{
private static FWatch fw;
private static string fileName;
private static string pathToFile;
private static string pathToDirectory;
public EventsViewModel()
{
_startCommand = new RelayCommand(OpenFileCommand);
}
private ICommand _startCommand;
public ICommand StartCommand
{
get { return _startCommand; }
}
private static ObservableCollection<Event> _eventsCollection = new ObservableCollection<Event>();
public static ObservableCollection<Event> EventsCollection
{
get { return _eventsCollection; }
}
private static string _buttonContent = "Open file";
public string ButtonContent
{
get { return _buttonContent; }
set
{
_buttonContent = value;
NotifyPropertyChanged();
}
}
private bool _pending = false;
public bool Pennding
{
get { return _pending; }
set
{
_pending = value;
NotifyPropertyChanged();
}
}
private void OpenFileCommand()
{
// Here I want to update field _pennding - is it right? Or should I delegate it?
// Should I update `Pendding` property of `ViewModel` where is command's method or I should do it in behind-code of view?
}
}
您应该从命令处理程序设置您的 "Pending" 属性。在您的情况下,它是 OpenFileCommand。
private void OpenFileCommand()
{
this.Pending = true;
}
您必须使用一些命令基础结构组件。有几个可用。 例如 MVVM Light 就是一个不错的选择。 看看这里的一些提示: How to use RelayCommand with the MVVM Light framework
但是您需要在表单上添加一个按钮,并将其绑定到命令以触发 ViewModel
上的操作。
Pending
应该在ViewModel
中进行操作。绑定负责其余的工作。
要引发 "property changed" 通知,我使用:
this.OnPropertyChanged(() => Breedte);
即改变的 属性 在最终引发的事件中被传递。我在你的代码中没有看到这一点。 你至少需要类似上面的东西或者
NotifyPropertyChanged("propertyName");
否则框架不知道改变了什么以及如何调整GUI。
好的,我知道了。
使用 xaml 中的数据上下文创建实例。我使用了 dataconytext 标签 3 次,我有 3 个 viemodel 实例。