【发布时间】: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; }
}
【问题讨论】: