【发布时间】:2016-05-15 17:29:53
【问题描述】:
有没有办法在视图模型中声明依赖属性?我想在 viewmodel 中声明一个依赖属性并通过命令更改它的值。
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 在当前上下文中不存在”。有没有其他方法可以做到这一点?
【问题讨论】:
-
为什么它必须是依赖属性?视图模型中的常规属性应该足够了。
-
我有两个视觉状态,我想根据依赖属性值和数据触发行为在这些状态之间切换。我想使用调用命令操作来改变依赖属性的值,从而在状态之间切换
标签: mvvm prism dependency-properties