如何将 ComboBox 转换为使用绑定的 CompositeCollection? [英] How do I convert a ComboBox to use a bound CompositeCollection?

查看:18
本文介绍了如何将 ComboBox 转换为使用绑定的 CompositeCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有绑定项目源的 ComboBox...我已经将我的示例剥离到关键部分:

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?

推荐答案

在 ComboBox.Resources 中声明 CompositeCollection 并将其与 ItemsSource="{Binding Source={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 的 Binding 将找不到其 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 这样的 Freezable 继承了它们声明元素的 DataContext,就好像它们是元素的逻辑子元素一样.但是,这是 Resources 属性和 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.

这篇关于如何将 ComboBox 转换为使用绑定的 CompositeCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆