2017-08-01 47 views
-1

我想列出Listbox控件中的項目。 從抽象基類派生的項目。 另外,我需要支持雙向綁定。c#winforms - 列表框中的繼承物品

基類名爲'動物'。 繼承的類叫做'Dog'和'Cat'並且具有獨特的屬性。 當在列表框中更改所選項目時,我想預覽派生項目的屬性。

基類:

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)); 
    } 
}  

Dog類

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"); 
     } 
    } 
} 

形式:

This is my form 表單代碼:

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); 

當我添加以下行的結合被破壞:

Binding ctx3 = new Binding("Text", listBox1.DataSource, "uniqueCatProp", true, DataSourceUpdateMode.OnPropertyChanged); 
     textBox3.DataBindings.Add(ctx3); 

我明白財產 「uniqueCatProp」 不是動物基地存在類,但我不認爲我應該添加另一個名爲「uniqueCatProp」的虛擬屬性到基類。

需要你的幫助。

+0

的喜歡綁定這麼多,爲什麼不使用wpf –

+0

這個問題只是我的應用程序的一個例子,已經在winforms中構建和實現。 我知道在這種情況下WPF更適合,但是我想知道我們應該如何在winforms中解決這個需求。 – Liran

+0

改爲使用事件 –

回答

0

動物類型看不到_uniqueCatProp OR uniqueCatProp

listBox1.DataSource必須是貓工作

Animal a = new Cat(); 

a.uniqueCatProp // ERROR since Animal has no uniqueCatProp 

的解決方案是,如果你uniqueCatProp添加到動物或使數據源類型貓