C# 使用Fody.PropertyChanged,如何与BindingList的属性一起使用<;T>;?
我使用Fody.PropertyChanged编写了以下类:C# 使用Fody.PropertyChanged,如何与BindingList的属性一起使用<;T>;?,c#,bindinglist,fody,fody-propertychanged,C#,Bindinglist,Fody,Fody Propertychanged,我使用Fody.PropertyChanged编写了以下类: [ImplementPropertyChanged] public class OtherClass : INotifyPropertyChanged { #pragma warning disable 67 public event PropertyChangedEventHandler PropertyChanged; #pragma warning restore 67 public int S
[ImplementPropertyChanged]
public class OtherClass : INotifyPropertyChanged
{
#pragma warning disable 67
public event PropertyChangedEventHandler PropertyChanged;
#pragma warning restore 67
public int SomeValue { get; set; }
}
[ImplementPropertyChanged]
public class MyClass : INotifyPropertyChanged
{
#pragma warning disable 67
public event PropertyChangedEventHandler PropertyChanged;
#pragma warning restore 67
public string SomeText { get; set; }
public BindingList<OtherClass> Others { get; private set; }
public MyClass ()
{
Others = new BindingList<OtherClass>();
}
}
[ImplementPropertyChanged]
公共类其他类:INotifyPropertyChanged
{
#pragma警告禁用67
公共事件属性更改事件处理程序属性更改;
#pragma警告恢复67
公共int SomeValue{get;set;}
}
[实现属性更改]
公共类MyClass:INotifyPropertyChanged
{
#pragma警告禁用67
公共事件属性更改事件处理程序属性更改;
#pragma警告恢复67
公共字符串SomeText{get;set;}
公共绑定列表其他{get;private set;}
公共MyClass()
{
Others=新绑定列表();
}
}
从使用MyClass的类中,我没有收到属性更改事件
这里有什么问题?尝试添加BindingList类的ListChanged事件以引发PropertyChanged事件:
public MyClass() {
this.Others = new BindingList<OtherClass>();
this.Others.ListChanged += Others_ListChanged;
}
void Others_ListChanged(object sender, ListChangedEventArgs e) {
if (this.PropertyChanged != null) {
this.PropertyChanged(this, new PropertyChangedEventArgs("Others"));
}
}
[ImplementPropertyChanged]
public class OtherClass {
public int ID { get; set; }
}
不熟悉Fody,但从中可以看出,Fody为您处理INotifyPropertyChanged-尝试删除INotifyPropertyChanged继承和事件调用。只是猜测而已。@LarsTech,如果我删除INotifyPropertyChanged继承,那么消费类就不能订阅PropertyChanged事件!我假设您对
Others
属性有问题,而不是SomeText
属性有问题?@Stécy当绑定列表内容更改时,您希望引发PropertyChanged
谁(哪个对象)以及使用什么参数(属性名称)?@BatteryBackupUnit我希望MyClass引发PropertyChanged(“Others”)(Fody可以在其他人上注册ListChanged事件)。好的,这负责传播PropertyChanged事件。如果Fody能够自动处理此情况,那就太好了。我仍然需要其他类来实现INotifyPropertyChanged,因为我希望其他类订阅该事件。@Stécy PropertyChanged.Fody不会实现它,因为它基本上是错误的。PropertyCha如果为属性分配了另一个值(在您的情况下,为Others
属性指定了另一个列表),则应引发nged。如果集合元素发生更改,则集合本身应引发事件。因此,如果您需要知道集合项是否发生更改,则需要同时订阅这两个值,PropertyChanged
(如果集合被替换)和其他。集合已更改
或其他。列表已更改
@BatteryBackupUnit我理解您的观点。感谢您的澄清。