簡體   English   中英

WPF組合框僅顯示某些項目

[英]WPF Combobox display only SOME items

因此,我有一個WPF應用程序(帶有MVVM),並且在其中有一個組合框,該組合框綁定到數據庫中的表並顯示值,這很好用。

但是,現在我想制作一個新的組合框並將其綁定到同一張表,但是現在我只希望它顯示一些值。 有沒有簡單的方法可以做到這一點?

該表有四個條目,但是我只想在此新組合框中顯示其中的三個。

我知道我可以在數據庫中創建一個新表進行綁定,但是我可能不得不使用其中幾個組合框(具有不同的值),如果可以避免的話,我寧願不經歷所有麻煩。

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; }
}

實現此目的的方式是,我將要在我的ItemsSource綁定到的屬性中過濾掉不想顯示在getter中的項目。

XAML:

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

在您的ViewModel中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM