WPF Combobox仅显示某些项目

时间:2018-03-27 17:17:12

标签: c# wpf xaml

所以我有一个WPF应用程序(使用MVVM),在这里我有一个组合框绑定到我的数据库中的表并显示值,这很好用。

但是,现在我想创建一个新的组合框并将其绑定到同一个表,但现在我只希望它显示一些值。有一种简单的方法可以做到这一点吗?

该表有四个条目,但我只想在这个新的组合框中显示其中的三个。

我知道我可以在数据库中创建一个新表来绑定,但我可能必须使用其中几个组合框(具有不同的值),如果我能避免它,我宁愿不去解决所有问题。 。

XAML:

private IEnumerable<ComponentLookupDto> _selectedComponentLookup;
    public IEnumerable<ComponentLookupDto> SelectedComponentLookup
    {
        get { return _selectedComponentLookup; }
        set
        {
            _selectedComponentLookup = value;
        }
    }

视图模型:

public class ComponentLookupDto
{
    public int ComponentLookupId { get; set; }
    public string ComponentChoice { get; set; }
}

DTO:

path=/home/new/abc/xyz

1 个答案:

答案 0 :(得分:1)

我实现这一点的方法是,我过滤掉我不想在getter中显示的项目,以便绑定我的ItemsSource。 :

XAML:

<ComboBox ItemsSource={Binding SelectedComponentLookupOther} ... />

在你的ViewModel中:

public IEnumerable<ComponentLookupDto> SelectedComponentLookupOther
{
    get { return _selectedComponentLookup.Where(c => c.SomeProperty == "however you want to pick it out"); }
}