바인딩에 대한 EntitySet 은 값을 표시하지 않는가?
-
10-07-2019 - |
문제
나는 바인딩 ItemsControl 는 EntitySet WPF 응용 프로그램에서.의 행동으로 예상되,그러나입니다.ItemsControl 의로 작동하는 경우 캐싱 의 내용 EntitySet 사 묶!
여기에 제거운 코드:
Entity:
public partial class Item : INotifyPropertyChanging, INotifyPropertyChanged
{
private EntitySet<Item> _Children;
public EntitySet<Item> Children {get{return _children;}}
/*...*/
}
나의 부분 클래스:
public partial class Item
{
public void RemoveChild(Item child)
{
Children.Remove(child);
// this finds PropertyChanged; defined in the Entity class
SendPropertyChanged("Children");
}
}
UI:
<ItemsControl
Name="ItemChildren"
Background="CornflowerBlue"
ItemsSource="{Binding Children}">
<ItemsControl.ItemTemplate>
<DataTemplate
DataType="{x:Type d:Item}">
<DockPanel>
<TextBlock
Text="{Binding Name}/>
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
코드는 동작을 보여 주 (가정>2 항목,각각의 단위):
this.ScrewyItem = Db.Items.First();
this.DataContext = ScrewyItem;
return;
나중에,내가 아이를 제거에서 entity:
ScrewyItem.RemoveChild(ScrewyItem.Children.First());
return;
이 코드를 실행한 후 실 UI 업데이트하지 않고 항목은 다음과 같은 모든 아이들이다.이 메소드를 호출 NotifyPropertyChanged,그래서인해야 합니다.
나중에 우리는 이 항목에서 UI 인:
this.DataContext = Db.Items.Last(); //different item
return;
그 다음에 바인딩하 UI 를 나중에 다시
this.DataContext = ScrewyItem;
당신이 생각하는 것도 이 시점에서 UI 용하여 올바른 아이 목록입니다. 이 지 케이스, 그러나!그것을 보여줍니다 동 목록 어린이로 원래 표시됩니다!
무엇이 더 기괴한 경우,자가점에서 내 아이들이 접근,내가 볼 수 있는 목록에 액세스할 때 내가 다시 바인딩을 UI 고 목록에 포함되지 않은 아이가 제거됩니다.
유일한 방법으로 나가는 것을 볼 수 있습 UI 될 수 있는 이것을 하는 경우의 컨텐츠 컬렉션 아이들 캐시되는 사이에 바인딩합니다.
여기에 무슨?나는 무엇???
해결책
내가 찾는 이것에 대한 해결책이 있습니다.그것의 종류이다.
I 할 수 있는 포장은 아이들이 컬렉션에서 BindingList:
public IBindingList BindableChildren
{
get
{
return new BindingList<Item>(Children);
}
}
그 다음 이를 수정 나의 방법을 쓰는 방법 나옵
this.Children.Remove(arg);
SendPropertyChanged("BindableChildren");
이 결과를 UI 에서 목록을 업데이트를 즉시 정지 표시를 캐싱 행동입니다.
질문은 여전히 remans: WTF? 고 또 다른 방법은 무엇입니까?
다른 팁
나는 이것에 대해 확실하지 않지만 아마도 ItemsControl
이벤트를 듣고 있습니다 IBindingList
또는 INotifyCollectionChanged
그리고 아닙니다 INotifyPropertyChanged
.