从一项到另一项时,我丢失了选定的值。我的组合框是这样的:
<local:ComboBoxEx x:Name="MYCB"
ItemsSource="{Binding Committente}"
SelectedValue="{Binding CommittenteSelected, Mode=TwoWay}"
SelectionChanged="committente_SelectionChanged"
/>
Committente 来自 ViewModel :
private ObservableCollection<CommittenteV> _Committente;
public ObservableCollection<CommittenteV> Committente
{
get { return _Committente; }
set
{
_Committente = value;
RaisePropertyChanged("Committente");
}
}
在我的 View 模型的构造函数中,我的集合是这样完成的:
Committente = ObservableCollectionConverter.GetObservableCollection<CommittenteV>(Service.getList);
Committente.Insert(0, null);
我添加了一个空项目,以便能够拥有一个象征“所有项目”的项目
我的问题是,从“真实”项目到这个 null 项目,我没有得到 CommittenteSelected 等于 null 而是选择的先例项目的值。
如果这不起作用,我怎么能体面地实现这个“所有项目”功能来提供我的过滤器的组合框?
谢谢你的帮助,
最佳答案
您可以将静态 CommittenteV 添加到 CommittenteV,而不是将其设置为 null,以返回 CommittenteV 的“未指定/全部”实例。类似于:
public static CommittenteV AllCommittente = new CommittenteV { Id = SomeUniqueValue };
然后检查 this 而不是 null。当您将其添加到列表中时:
Committente.Insert(0, CommittenteV.AllCommittente);
也许给那个“AllCommittente”一个永远不会被任何“真正的”Committente使用的唯一ID?它还允许您为此 AllCommittente 条目创建特定的显示标签(如果您有一个用于在组合框中显示值的属性)。
关于c# - 组合框的 SelectedValue 不反射(reflect)传递给空项时的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7350863/