2013-05-07 2 views
0

Я пытаюсь создать базовый класс, который позволит различным элементам управления связывать и отображать некоторые значения.Простая реализация сообщения о состоянии

Я хочу иметь статический список объектов, где каждый объект имеет некоторые свойства, такие как подпись, тики счетчик, что угодно.

Затем я хочу привязать ярлык к последнему добавленному элементу к этому списку и datagridview, чтобы увидеть их все.

Было бы здорово, если бы такое решение могло быть как для winforms, так и для сред wpf.

Если бы вы могли указать мне, что я делаю неправильно. Благодарю.

Проект идеи (один из многих уже протестированных и не выполненных) ниже. Класс

Статус:

public class Status: INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    //implementation of observable collection 
    private static ObservableCollection<Status> _list; 
    public static ObservableCollection<Status> List 
    { 
     get { return _list ?? (_list = new ObservableCollection<Status>{new Status()}); } 
    } 

    //object properties 
    public string Message { get; set; } 
    public bool Finished { get; set; } 

    //object views 
    public string View 
    { 
     get { return Message + "(" + Finished + ")" ; } 
    } 

    //object methods 
    public static Status Add(string message) 
    { 
     var result = new Status 
     { 
      Message = message, 
      Finished = false 
     }; 

     List.Add(result); 
     return result; 
    } 

    public void Finish() 
    { 
     Finished = true; 
    } 
} 

форма:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     label1.DataBindings.Add("Text", Status.List, "Message"); 
     listBox1.DisplayMember = "View"; 
     listBox1.DataSource = Status.List; 
    } 

    private void Button1_Click(object sender, EventArgs e) 
    { 
     label5.Text = Status.Add(textBox1.Text).Message; 
     textBox1.Text = ""; 
    } 

    private void Button2_Click(object sender, EventArgs e) 
    { 
     ((Status)listBox1.SelectedItem).Finish(); 
    } 
} 

ответ

0

Это сделал трюк:

using System; 
using System.ComponentModel; 
using System.Drawing; 
using System.Linq; 
using System.Runtime.CompilerServices; 
using WindowsFormsApplication2.Annotations; 

namespace WindowsFormsApplication2 
{ 

public class Item : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    #region Notyfier implementation 
    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion 

    #region collection implemetation 

    public BindingList<Item> Items = new BindingList<Item>(); 

    public string Count 
    { 
     get { return (Items.Count == 1) 
      ? "1 item." 
      : Items.Count + " items."; } 
    } 

    public Item Current 
    { 
     get { return Items.Count == 0 
      ? new Item {Colour = Color.Chartreuse} //default initial item 
      : Items.Last(); } 
    } 
    #endregion 

    #region object implemetation 

    protected object ID { get; set; } 
    public Color Colour { get; set; } 

    public void NewItem(Color color) 
    { 
     Items.Add(new Item 
      { 
       ID = Guid.NewGuid(), 
       Colour = color 
      }); 

     OnPropertyChanged("Count"); 
     OnPropertyChanged("Current"); 
    } 

    #endregion 
} 
} 
Смежные вопросы