How to convert a ComboBox to use a bound CompositeCollection?

I have a ComboBox that has an associated source of elements ... I have divided my example into key parts:

<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> 

This works, but now I want to add one manual element to my combobox that will run alternative functions called "Other ...", so I need to move on to using CompositeCollection ... like this:

 <ComboBox Name="Options" DataContext="ClientsCollection"> <ComboBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding [ClientNumber], Converter={StaticResource ClientOptions} /> <ComboBoxItem>Other...</ComboBoxItem> </CompositeCollection> </ComboBox> 

Try as I could, related items just won't fill when using CompositeCollection. It shows only the manual ComboBoxItem "Other ...". If I delete this item, the list is empty. If I tie a breakpoint to the converter, it will not catch anything, which indicates that the binding did not even try.

I obviously don’t understand how the binding function in CompositeCollection happens. Can someone see the error in my XAML or explain what I am missing?

+1
source share
1 answer

Declare a 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}}" /> 

If you declare a CompositeCollection inside the ItemsSource property in the syntax of the elements, the Binding for the CollectionContainer.Collection does not find its DataContext.

In the Resources section, Freezables, such as CompositeCollection, inherit the DataContext of their declaring element, as if they were logical children of this element. However, this is a specialty for properties and resource properties, such as ContentControl.Content or similar properties that contain logical children of the control (and possibly several others). If you use element syntax to set a property value, in general, you should expect that inheriting property properties for properties such as a DataContext does not work, and therefore bindings without an explicit source will not work either.

+1
source

Source: https://habr.com/ru/post/1483597/


All Articles