2016-08-26 3 views
0

공용 컨트롤 사용자 목록이 포함 된 Postbox이라는 사용자 정의 컨트롤을 만들었습니다.C# 목록에 개체를 추가하는 동안 무언가 수행

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace DocEngineManager 
{ 
    public partial class PostBox : UserControl 
    { 
     public PostBox() 
     { 
      InitializeComponent(); 
     } 

     public List<PostBoxPost> Posts = new List<PostBoxPost>(); 

    } 
} 

PostBoxPosts이 목록에 포함 된 UserControl을의 유형입니다 :

여기 내 코드입니다.

사용자가 자신의 응용 프로그램에 Posts 목록에 PostBoxPost을 추가하면 추가 된 내용을 알고있는 내 UserControl 클래스 내부에서 발생시키고 이벤트를 발생시키고 싶습니다.

+2

더 간단한 용어로 무엇을 달성해야하는지 설명해야합니다. – Aybe

+0

'Postbox '가 컨트롤이므로'List '이 쓸모없는 것처럼 보입니다. 유용 할 수 있도록 부모의'Controls' 콜렉션에 컨트롤이 추가되어야합니다. * 1) * 목표는 무엇입니까? * 2) * 런타임 지원 만 필요합니까? 아니면 디자인 타임 지원이 필요할 수도 있습니다. –

+0

디자인 타임과 런타임 지원이 필요합니다. 아래 답변 중 아직까지 도움이되지 않았습니다. 제어가 추가되고 제어가 추가되어서 추가되는 동안 또는 편집이 추가 된 직후에 편집 할 수 있는지를 알아야합니다. –

답변

4

간단한 목록으로 어떤 이벤트도 노출되지 않습니다. ObservableCollection과 같은 것이 효과가 있습니까?

public partial class PostBox : UserControl 
{ 
    public ObservableCollection<PostBoxPost> Posts = new ObservableCollection<PostBoxPost>(); 

    public PostBox() 
    { 
     InitializeComponent(); 

     Posts.CollectionChanged += OnPostsCollectionChanged; 
    } 

    private void OnPostsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     if (e.NewItems != null && e.NewItems.Count != 0) 
     { 
      foreach (PostBoxPost postBoxPost in e.NewItems) 
      { 
       // Do custom work here? 
      } 
     } 
    } 
} 
+0

ObserveableCollection은 일종의 WPF입니다. BindingList는 더 많은 Winforms 버전입니다. –

+0

이 방법은 저에게 효과적입니다. 컬렉션에서 추가 된 항목을 찾아서 Posts.Last(). Parent = true;를 입력하여 PostBox를 부모로 만들 수있었습니다. –

관련 문제