如何转换一个ComboBox使用绑定CompositeCollection?
我已经具有绑定物品源的组合框...我已经剥夺我的例子下降到关键件:
I have a ComboBox that has a bound items source... I've stripped my example down to the key pieces:
<UserControl x.Class="My.Application.ClientControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conv="clr-namespace:My.Utilities.Converters"
Name="ClientControl">
<UserControl.Resources>
<ResourceDictionary>
<CollectionViewSource Key="x:ClientsCollection" />
</ResourceDictionary>
<conv:ClientOptions x:Key="ClientOptions" />
</UserControl.Resources>
...
<ComboBox Name="Options"
DataContext="ClientsCollection"
ItemsSource="{Binding [ClientNumber], Converter={StaticResource ClientOptions}" />
</UserControl>
这工作,但我现在想添加一个手工物品到我的组合框,将触发所谓的替代功能其他...所以我不必移动使用CompositeCollection ......像这样:
This works, but I now want to add a single manual item to my combobox that will trigger alternate functionality called "Other..." so I'm having to move to using the CompositeCollection... like so:
<ComboBox Name="Options"
DataContext="ClientsCollection">
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding [ClientNumber], Converter={StaticResource ClientOptions} />
<ComboBoxItem>Other...</ComboBoxItem>
</CompositeCollection>
</ComboBox>
尝试,因为我可能,绑定的物品是不会使用CompositeCollection时填充。它只能说明手册ComboBoxItem其他...。如果我删除该项目,列表是空的。如果我附上一个断点到转换器不捉任何东西,这似乎表明绑定甚至没有尝试。
Try as I might, the bound items just won't populate when using the CompositeCollection. It only shows the manual ComboBoxItem "Other...". If I remove that item, the list is empty. If I attach a breakpoint to the converter it doesn't catch anything, which seems to indicate that the binding isn't even attempted.
我显然不理解的东西如何在CompositeCollection绑定功能正在发生的事情。有人可以看到一个错误在我的XAML或解释我缺少的是什么?
I am obviously not understanding something about how the binding function in the CompositeCollection is happening. Can someone see an error in my XAML or explain what I'm missing?
在声明中CompositeCollection和ComboBox.Resources与ItemsSource的使用={绑定源= {StaticResource的myCompositeCollection}}。
Declare the CompositeCollection in ComboBox.Resources and use it with ItemsSource="{Binding Source={StaticResource myCompositeCollection}}" .
<UserControl x.Class="My.Application.ClientControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conv="clr-namespace:My.Utilities.Converters"
Name="ClientControl">
<UserControl.Resources>
<ResourceDictionary>
<CollectionViewSource Key="x:ClientsCollection" />
</ResourceDictionary>
<conv:ClientOptions x:Key="ClientOptions" />
<CompositeCollection x:Key="myCompositeCollection">
<CollectionContainer Collection="{Binding Source={StaticResource ClientsCollection}, Path=[ClientNumber], Converter={StaticResource ClientOptions} />
<ComboBoxItem>Other...</ComboBoxItem>
</CompositeCollection>
</UserControl.Resources>
...
<ComboBox Name="Options"
DataContext="ClientsCollection"
ItemsSource="{Binding Source={StaticResource myCompositeCollection}}" />
如果您在声明元素语法ItemsSource属性里面的CompositeCollection中,为CollectionContainer.Collection绑定没有找到它的DataContext的。
If you declare the CompositeCollection inside the ItemsSource property in element syntax, the Binding for the CollectionContainer.Collection doesn't find its DataContext.
参考资料部分里面,像CompositeCollection Freezables继承其声明元素的DataContext的,就好像它们是元素的逻辑孩子。然而,这是资源属性和属性,如ContentControl.Content或包含控制逻辑的孩子(和其他人也许几个)类似属性的特产。如果你使用元素语法来设置属性的值,一般你将不得不指望像的DataContext属性属性值继承不工作,所以没有明确的来源绑定将无法正常工作,无论是。
Inside the Resources section, Freezables like CompositeCollection inherit the DataContext of their declaring element, as if they were logical children of the element. However, this is a speciality of the Resources property and properties like ContentControl.Content or similar properties which contain the logical children of a control (and maybe a few others). If you use element syntax to set the value of a property, in general you would have to expect that property value inheritance for properties like DataContext doesn't work, and so Bindings without an explicit Source won't work, either.