CollectionContainer 不绑定我的 Collection

本文关键字:Collection 我的 绑定 CollectionContainer | 更新日期: 2023-09-27 18:36:50

我有一个类似于表单的东西,用户可以在其中为车辆分配一个客户。但这是可选的。客户在组合框中。我需要一种方法来确保如果组合框打开,用户可以选择"无"。

如果发现类似这样的东西:

<ComboBox.ItemsSource>    
    <CompositeCollection>
       <ComboBoxItem IsEnabled="False" Foreground="Black">none</ComboBoxItem>
       <CollectionContainer Collection="{Binding Source={StaticResource DataKey}}" />    
    </CompositeCollection>
</ComboBox.ItemsSource>

但是绑定到集合对我不起作用。

我的窗体是包含在 WPF 窗口中的用户控件。数据上下文位于窗口中的网格上。因此,我尝试了:

<CollectionContainer Collection="{Binding Customers, Source={RelativeSource AncestorType=Grid}}" />

但组合框只显示"无"。如何解决我的问题?

提前感谢!

CollectionContainer 不绑定我的 Collection

假设你在这里拿了代码:带有空项的组合框?

答案是绑定不起作用,因为它没有访问权限。如果要修复它,请使用BindingProxy:http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/