【问题标题】:WPF Combobox display only SOME itemsWPF Combobox 仅显示某些项目
【发布时间】:2018-09-06 05:09:49
【问题描述】:

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

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

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

我知道我可以在数据库中创建一个新表来绑定,但我可能不得不使用其中几个组合框(具有不同的值),如果我可以避免的话,我宁愿不费心.

XAML:

<ComboBox   
            Name="cmComp"
            MinWidth="150"
            Margin="12 0 0 12"
            ItemsSource="{Binding SelectedComponentLookup}"
            DisplayMemberPath="ComponentChoice"
            SelectedValuePath="ComponentChoice"
            SelectedItem="{Binding ComponentChosen}">
</ComboBox>

视图模型:

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

DTO:

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

【问题讨论】:

    标签: c# wpf xaml


    【解决方案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"); }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-14
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 2013-07-11
      • 2012-01-27
      • 2014-09-01
      • 2011-04-19
      相关资源
      最近更新 更多