【发布时间】:2015-04-14 08:58:48
【问题描述】:
ICommand接口如何与MVVM配合使用?
鉴于我添加了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?
}
}
【问题讨论】:
-
是的,它是正确的,OpenFileCommand 已经是传递给 RelayCommand 的委托,这正是它应该使用的方式。使用 RelayCommand 时要注意的一件事(我猜它来自 MVVMlight 或原始 Josh Smith 实现)它应该在 ViewModel 被释放时设置为 null,因此 ViewModel 应该实现 IDisposable,如果你使用来自 MS Prism 的 DelegateCommand 那么你不会不需要实现 IDisposable 是否在内部对事件处理程序使用弱引用
-
@AwkwardCoder 但是如何在此方法中更改 Pennding 属性?
-
如下图@XAML Lover
-
你应该有类似 NotifyPropertyChanged("Pennding");请注意,我在此处的代码中具有与您的代码相同的属性名称,而不是“待定”。
-
@AwkwardCoder 但它对我不起作用。没有什么变化。我想我应该在进度条数据上下文中使用 x:Name - 这给了我 EventViewModel 的实例。