WPF ItemsControl:로 TextChanged 이벤트 모든 항목을 정렬
-
06-07-2019 - |
문제
나는 배치에서 ItemsControl 컨테이너(템플릿 Stackpanel)내 UserControls 는 동적으로 추가되고 제거하는 응용 프로그램을 실행하는 동안.할 수 있는 방법있는 이벤트가(예를들면TextChanged 또는 GotFocus)모든 요소에 정렬(주로 구성되어 있의 텍스트)?이는 내가 사용해야 한다"대리인"또는 ICommand?나는 새로운 그리고 아마도 내가 혼합 몇 가지 있습니다.
감사합니다!
해결책
내가 읽기 사이의 노선 당신의 질문이 꽤 있지만,의심할을 첨부(및 분리)이벤트 처리기의 각각의 제어 아이들은 추가(및 제거됨).
설정하고 ItemsSource 는다.에 연결할 수 있는 이벤트 핸들러를 당신의습니다.CollectionChanged 이벤트.에서 말했 이벤트 핸들러를 연결하거나 분리할 수 있습니다 이벤트에 핸들러를 당신의 아이들이 추가되고 제거됩니다.
public class MyContainer : StackPanel
{
public MyContainer()
{
this.ItemsSource = MyCollection;
}
ObservableCollection<UIElement> myCollection;
public ObservableCollection<UIElement> MyCollection
{
get
{
if (myCollection == null)
{
myCollection = new ObservableCollection<UIElement>();
myCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(myCollection_CollectionChanged);
}
return myCollection;
}
void myCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
foreach (UIElement removed in e.OldItems)
{
if (added is TextBox)
(added as TextBox).TextChanged -= new Removeyoureventhandler here...
if (added is someotherclass)
(added as someotherclass).someotherevent += Removesomeothereventhandler here...
}
foreach (UIElement added in e.NewItems)
{
if (added is TextBox)
(added as TextBox).TextChanged += new Addyoureventhandler here...
if (added is someotherclass)
(added as someotherclass).someotherevent += Addsomeothereventhandler here...
}
}
제휴하지 않습니다 StackOverflow