[英]Can't change ComboBox selection when bound to ObservableCollection (WPF)
[英]WPF Combobox selection disappears when the an item is deleted in the list bound to combobox
我有一個組合框
<ComboBox x:Name="HeadComboBox" ItemsSource="{Binding DataContext.HeadList, RelativeSource={RelativeSource FindAncestor,AncestorType= {x:Type views:FixedAssetBaseWholeUC}}}" Margin="195,78,86,0" VerticalAlignment="Top" SelectedItem="{Binding HeadItem}" DisplayMemberPath="Name" />
datacontext.HeadList將指向:
public List<FixedAssetHeadItem> HeadList
{
get
{
return _headList;
}
set
{
if (_headList != value)
{
_headList = value;
RaisePropertyChanged("HeadList");
}
}
}
我禁用了組合框所在的UserControl並加載了另一個控件,以通過以下方式編輯標題列表中的項目
DeleteFromHeadList(1);
FixedAssetBaseWholeViewModel fbwvm = (FixedAssetBaseWholeViewModel)Fabwuc.DataContext;
fbwvm.HeadList = HeadList;
編輯完成后,重新啟用用戶控件,僅找到選擇分配器。
調試顯示http://postimg.org/image/hdz4h4px3/
我該如何處理?
您不應綁定到List( 可能導致內存泄漏 ),而應綁定到ObservableCollection<>
。 這樣,您的ComboBox應該會適當更新。 另外,您的HeadItem
應該是INPC屬性-在setter中(私有或公共,取決於您的代碼)應引發屬性更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.