【发布时间】:2019-04-17 11:18:15
【问题描述】:
在我的 CustomControl 中,我有 View 依赖属性。使用此控件的外部控件通过视图模型设置此属性。设置后,会触发 Refresh 方法并渲染视图!
到目前为止一切都很好!但是,如果我还希望它在 View 的属性发生更改时刷新。
也许我不是按照标准方式做的?我应该在控件上定义一个公共 Refresh() 方法并从外部调用它吗?如何使用指挥?
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register(
"View", typeof(View),
typeof(CustomControl), new PropertyMetadata(Refresh)
);
public View View
{
get => (View)GetValue(ViewProperty);
set => SetValue(ViewProperty, value);
}
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//
MessageBox.Show("Refreshed!");
}
public sealed class View : INotifyPropertyChanged
{
private bool m_isDirty;
public bool IsDirty
{
get => m_isDirty;
set
{
m_isDirty = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】:
标签: c# wpf mvvm dependency-properties inotifypropertychanged