在我的CustomControl中,我具有View依赖项属性。使用此控件的外部控件通过视图模型设置此属性。设置后,将触发Refresh方法并呈现View!
到目前为止一切都很好!但是,如果我还希望它在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));
}
}
答案 0 :(得分:1)
您可以在回调中为PropertyChanged
的{{1}}事件连接事件处理程序:
View