带有静态和动态数据的组合框 [英] Combobox with static and dynamic data
问题描述
我想创建一个绑定到动态资源的组合框,并有一个位于顶部的自定义 xaml 预先指定的列表.我知道如何将组合框绑定到动态资源,
I would like to create a combobox that binds to a dynamic resource and has a custom xaml pre-specified list that stays at the top. I know how to bind a combobox to a dynamic resource,
<ComboBox Name="comboBox1" Width="Auto" ItemsSource="{Binding}" />
而且我知道如何插入静态项目(参见 WPF - 添加静态项目到组合框).
and I know how to insert static items (see WPF - add static items to a combo box ).
<ComboBox Text="Is not open">
<ComboBoxItem Name="cbi1">Item1</ComboBoxItem>
<ComboBoxItem Name="cbi2">Item2</ComboBoxItem>
<ComboBoxItem Name="cbi3">Item3</ComboBoxItem>
</ComboBox>
但我不知道如何同时进行?
but I don't know how to do both at the same time?
注意:显然,使用自定义小部件等有很多不同的方法可以做到这一点,我只是觉得这应该很容易.
Note: obviously, there are plenty of different ways to do this with custom widgets and the like, I just feel this should be quite easy somehow.
推荐答案
CompositeCollection 对此非常酷.像这样:
CompositeCollection is pretty cool for this. Something like so:
<ComboBox>
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={...whatever...}" />
<ComboBoxItem Name="cbi1">Item1</ComboBoxItem>
<ComboBoxItem Name="cbi2">Item2</ComboBoxItem>
<ComboBoxItem Name="cbi3">Item3</ComboBoxItem>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
这篇关于带有静态和动态数据的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!