[英]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.