WPF 中的依赖项 属性 和 属性ChangedCallback 存在问题
Problem with Dependency Property and PropertyChangedCallback in WPF
在我的 CustomControl 中,我有 View 依赖项 属性。使用此控件的外部控件通过视图模型设置此 属性。设置后,将触发 Refresh 方法并呈现 View!
到目前为止一切都很好!但是,如果我还希望它在视图的 属性 更改时刷新怎么办。
也许我没有按标准方式进行操作?我应该在控件上定义一个 public 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));
}
}
您可以在回调中为 View
的 PropertyChanged
事件连接事件处理程序:
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show("Refreshed!");
View newView = e.NewValue as View;
if (newView != null)
newView.PropertyChanged += NewView_PropertyChanged;
View oldView = e.OldValue as View;
if (oldView != null)
oldView.PropertyChanged -= NewView_PropertyChanged;
}
private static void NewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
View view = (View)sender;
//view updated...
}
在我的 CustomControl 中,我有 View 依赖项 属性。使用此控件的外部控件通过视图模型设置此 属性。设置后,将触发 Refresh 方法并呈现 View!
到目前为止一切都很好!但是,如果我还希望它在视图的 属性 更改时刷新怎么办。
也许我没有按标准方式进行操作?我应该在控件上定义一个 public 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));
}
}
您可以在回调中为 View
的 PropertyChanged
事件连接事件处理程序:
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show("Refreshed!");
View newView = e.NewValue as View;
if (newView != null)
newView.PropertyChanged += NewView_PropertyChanged;
View oldView = e.OldValue as View;
if (oldView != null)
oldView.PropertyChanged -= NewView_PropertyChanged;
}
private static void NewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
View view = (View)sender;
//view updated...
}