将一个空项目添加到有界组合框 [英] Add an empty item to a bounded combobox
问题描述
我需要在wpf mvvm应用程序的有界组合框中添加一个空项目,我尝试过
I need to add an empty item in a bounded combobox within wpf mvvm application, I tried this
<ComboBox TabIndex="23" Text="{Binding Specialisation}" DisplayMemberPath="refsp_libelle">
<ComboBox.ItemsSource>
<CompositeCollection >
<ComboBoxItem > </ComboBoxItem>
<CollectionContainer Collection="{Binding SpecialisationItemSource}" ></CollectionContainer>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
在我尝试添加空项目之前它就起作用了.
It worked before I tried to add the empty item.
<ComboBox TabIndex="23" Text="{Binding Specialisation}" ItemsSource="{Binding SpecialisationItemSource}" DisplayMemberPath="refsp_libelle"/>
所以我需要知道:
- 我犯了什么错误?
- 我该如何解决?
谢谢
推荐答案
为什么您的方法无效?
您使用{Binding SpecialisationItemSource}
,由于未明确定义绑定的源,因此回退到使用目标的DataContext
作为源-否则,如果CollectionContainer
是FrameworkElement
,则不是.因此,绑定的来源是null
,并且该组合中没有任何项目显示.您需要显式设置绑定的Source
属性以使其起作用(设置RelativeSource
或ElementName
也不起作用).
You use {Binding SpecialisationItemSource}
which, since no source for the binding is explicitly defined, falls back to using target's DataContext
as the source - or rather it would if CollectionContainer
was a FrameworkElement
, and it's not. Hence the source for the binding is null
and no items show up in the combo. You'd need to set the Source
property of the binding explicitly to make it work (setting RelativeSource
or ElementName
wouldn't work either).
实际上,即使 CollectionContainer
是 FrameworkElement
它仍然不起作用,因为 CompositeCollection
无效 FrameworkElement
(甚至还不是一个 DependencyObject
),因此数据上下文继承将被破坏).
Actually even if CollectionContainer
was a FrameworkElement
it still would not work, because CompositeCollection
is not a FrameworkElement
(it's not even a DependencyObject
), so the data context inheritance would be broken).
如何解决?
为了使用隐式绑定",您可以将CollectionViewSource
放在资源字典中,并通过使用StaticResource
扩展名将其填充到收集容器中:
In order to use the "implicit binding", you can place a CollectionViewSource
in a resource dictionary, and use that to fill the collection container by using StaticResource
extension:
<ComboBox>
<ComboBox.Resources>
<CollectionViewSource x:Key="Items" Source="{Binding SpecialisationItemSource}" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<TextBlock />
<CollectionContainer Collection="{Binding Source={StaticResource Items}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
请注意,我使用的是Collection="{Binding Source={StaticResource Items}}"
而不是Collection="{StaticResource Items}"
-这是因为类型为CollectionViewSource
的对象不是实际的集合,并且不是CollectionContainer.Collection
属性的有效值,并且绑定机制旨在将它变成一个实际的集合.另外,我用空的TextBlock
替换了空的ComboBoxItem
,因为前者会导致绑定错误,而我确实不喜欢看到这种错误.最终,我什至将其替换为绑定集合的项目类型的默认值.
Notice that I used Collection="{Binding Source={StaticResource Items}}"
instead of just Collection="{StaticResource Items}"
- that's because an object of type CollectionViewSource
is not an actual collection and is not valid value for the CollectionContainer.Collection
property, and binding mechanism is designed to turn it into an actual collection. Also, I replaced an empty ComboBoxItem
with an empty TextBlock
, because the former resulted in binding errors, which I really don't like seeing. Ultimately, I'd even go with replacing it with a default value for the bound collection's item type.
这篇关于将一个空项目添加到有界组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!