c#winforms-列表框中的继承项
我想在Listbox控件中列出项目。 从抽象基类派生的项。 另外,我需要支持双向绑定 基类称为“Animal”。 继承的类称为“Dog”和“Cat”,具有唯一的属性。 在列表框中更改选定项时,我希望预览派生项的属性 基类:c#winforms-列表框中的继承项,c#,winforms,inheritance,data-binding,listbox-control,C#,Winforms,Inheritance,Data Binding,Listbox Control,我想在Listbox控件中列出项目。 从抽象基类派生的项。 另外,我需要支持双向绑定 基类称为“Animal”。 继承的类称为“Dog”和“Cat”,具有唯一的属性。 在列表框中更改选定项时,我希望预览派生项的属性 基类: public abstract class Animal : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public string _
public abstract class Animal : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string _Name { get; set; }
public virtual string Name { get; set; }
private int _Id;
public int Id
{
get
{
return _Id;
}
set
{
_Id = value;
OnPropertyChanged("Id");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Binding ctx3 = new Binding("Text", listBox1.DataSource, "uniqueCatProp", true, DataSourceUpdateMode.OnPropertyChanged);
textBox3.DataBindings.Add(ctx3);
狗类
public class Dog : Animal
{
public override string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
}
public class Cat : Animal
{
public override string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
private string _uniqueCatProp;
public string uniqueCatProp
{
get
{
return _uniqueCatProp;
}
set
{
_uniqueCatProp = value;
OnPropertyChanged("uniqueCatProp");
}
}
}
猫类
public class Dog : Animal
{
public override string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
}
public class Cat : Animal
{
public override string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
private string _uniqueCatProp;
public string uniqueCatProp
{
get
{
return _uniqueCatProp;
}
set
{
_uniqueCatProp = value;
OnPropertyChanged("uniqueCatProp");
}
}
}
表格:
表格编号:
public Form1()
{
InitializeComponent();
BindingList<Animal> animals = new BindingList<Animal>();
Dog d1 = new Dog();
d1.Id = 1;
d1.Name = "dog1";
Dog d2 = new Dog();
d2.Id = 2;
d2.Name = "dog2";
Cat c1 = new Cat();
c1.Id = 3;
c1.Name = "cat1";
c1.uniqueCatProp = "clean";
animals.Add(d1);
animals.Add(d2);
animals.Add(c1);
BindingSource bs = new BindingSource();
bs.DataSource = typeof(Animal);
foreach (var item in animals)
{
bs.Add(item);
}
listBox1.DataSource = bs;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Name";
Binding ctx1 = new Binding("Text", listBox1.DataSource, "Id", true, DataSourceUpdateMode.OnPropertyChanged);
textBox1.DataBindings.Add(ctx1);
Binding ctx2 = new Binding("Text", listBox1.DataSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
textBox2.DataBindings.Add(ctx2);
我知道属性“uniqueCatProp”在Animal基类中不存在,但是我不认为应该向基类添加另一个名为“uniqueCatProp”的虚拟属性
需要您的帮助。动物类型看不到\u uniqueCatProp或uniqueCatProp listBox1.DataSource必须是Cat才能工作
Animal a = new Cat();
a.uniqueCatProp // ERROR since Animal has no uniqueCatProp
解决方案是将uniqueCatProp添加到Animal或将DataSource设置为Cat类型如果您非常喜欢绑定,为什么不使用WPF?这个问题只是我已经在winforms中构建和实现的应用程序的一个例子。我知道WPF在这种情况下更合适,但是,我想知道我们应该如何在winforms中解决这个需求。请改用事件