gpt4 book ai didi

wpf - 如何引发依赖属性上的属性更改事件?

转载 作者:行者123 更新时间:2023-12-03 05:35:36 24 4
gpt4 key购买 nike

我有一个具有两个属性的控件。一个是 DependencyProperty,另一个是第一个的“别名”。当第一个事件发生更改时,如何引发第二个事件(别名)的 PropertyChanged 事件。

注意:我使用的是DependencyObjects,而不是INotifyPropertyChanged(尝试过,但没用,因为我的控件是 ListVie 子类)

类似这样的事情......

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == MyFirstProperty)
{
RaiseAnEvent( MySecondProperty ); /// what is the code that would go here?
}
}

如果我使用 INotify,我可以这样做......

public string SecondProperty
{
get
{
return this.m_IconPath;
}
}

public string IconPath
{
get
{
return this.m_IconPath;
}
set
{
if (this.m_IconPath != value)
{
this.m_IconPath = value;
this.SendPropertyChanged("IconPath");
this.SendPropertyChanged("SecondProperty");
}
}
}

在哪里可以从一个 setter 引发多个属性的 PropertyChanged 事件?我需要能够做同样的事情,只使用 DependencyProperties

最佳答案

我遇到了类似的问题,我有一个依赖属性,我希望该类监听更改事件以从服务中获取相关数据。

public static readonly DependencyProperty CustomerProperty = 
DependencyProperty.Register("Customer", typeof(Customer),
typeof(CustomerDetailView),
new PropertyMetadata(OnCustomerChangedCallBack));

public Customer Customer {
get { return (Customer)GetValue(CustomerProperty); }
set { SetValue(CustomerProperty, value); }
}

private static void OnCustomerChangedCallBack(
DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
CustomerDetailView c = sender as CustomerDetailView;
if (c != null) {
c.OnCustomerChanged();
}
}

protected virtual void OnCustomerChanged() {
// Grab related data.
// Raises INotifyPropertyChanged.PropertyChanged
OnPropertyChanged("Customer");
}

关于wpf - 如何引发依赖属性上的属性更改事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2480366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com