Mvvm Prism中viewmodel内部的依赖项属性
有没有办法在viewmodel中声明依赖项属性?我想在viewmodel中声明一个依赖属性,并通过命令更改它的值Mvvm Prism中viewmodel内部的依赖项属性,mvvm,prism,dependency-properties,Mvvm,Prism,Dependency Properties,有没有办法在viewmodel中声明依赖项属性?我想在viewmodel中声明一个依赖属性,并通过命令更改它的值 public class MyViewModel : Prism.Windows.Mvvm.ViewModelBase { public bool IsPaneVisible { get { return (bool)GetValue(IsPaneVisibleProperty); } set {
public class MyViewModel : Prism.Windows.Mvvm.ViewModelBase
{
public bool IsPaneVisible
{
get { return (bool)GetValue(IsPaneVisibleProperty); }
set { SetValue(IsPaneVisibleProperty, value); }
}
public static readonly DependencyProperty IsPaneVisibleProperty =
DependencyProperty.Register("IsPaneVisible", typeof(bool), typeof(MyViewModel), new PropertyMetadata(0));
public ICommand VisibilityChangeCommand { get; set; }
public MyViewModel()
{
VisibilityChangeCommand = new DelegateCommand(OnVisibilityChange);
}
private void OnVisibilityChange()
{
IsPaneVisible = !IsPaneVisible;
}
}
问题是,我在IsPaneVisible的getter/setter中遇到一些编译错误:“当前上下文中不存在GetValue”。是否有其他方法可以做到这一点?在上使用了
dependencProperty
,例如UserControl
。Prism的ViewModelBase不是依赖对象,主要是因为这种类型是特定于平台的。为了支持viewmodel的绑定,我们通常使用INotifyPropertyChanged
Prism在基类中实现此接口,ViewModelBase也从基类派生。您可以这样定义属性:
private string _imagePath;
public string ImagePath
{
get { return _imagePath; }
set { SetProperty(ref _imagePath, value); }
}
如果安装Visual Studio扩展,则可以使用
propp
代码段。为什么它必须是依赖项属性?在视图模型中,一个常规属性就足够了。我有两个可视状态,我想根据具有数据触发器行为的依赖属性值在这些状态之间切换。我想使用Invoke命令操作,它将更改dependency属性的值,从而在状态之间切换