CompositeCollection / CollectionViewSource confusion

I got a little confused about how data binding works when using these types.

I read that you cannot do the following

public partial class Window1 : Window { public ObservableCollection<string> Items { get; private set; } public Window1() { Items = new ObservableCollection<string>() { "A", "B", "C" }; DataContext = this; InitializeComponent(); } } <Window x:Class="WpfApplication25.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ComboBox> <ComboBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding Items}"/> </CompositeCollection> </ComboBox.ItemsSource> </ComboBox> </Window> 

because the CompositeCollection has no concept of a datacontext, and therefore anything inside it using the binding should set the Source property. For example:

 <Window x:Class="WpfApplication25.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Window.Resources> <CollectionViewSource x:Key="list" Source="{Binding Items}"/> </Window.Resources> <ComboBox Name="k"> <ComboBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding Source={StaticResource list}}"/> </CompositeCollection> </ComboBox.ItemsSource> </ComboBox> </Window> 

But how does it work? it sets the source to something, but something, in this case the CollectionViewSource uses the datacontext (since it does not explicitly set the source).

So, because a β€œlist” is declared in Window resources, does this mean that it gets a Windows DataContext? In this case, why the following also do not work?

 <Window x:Class="WpfApplication25.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Window.Resources> <Button x:Key="menu" Content="{Binding Items.Count}"/> </Window.Resources> <ComboBox Name="k"> <ComboBox.ItemsSource> <CompositeCollection> <ContentPresenter Content="{Binding Source={StaticResource menu}}"/> </CompositeCollection> </ComboBox.ItemsSource> </ComboBox> </Window> 
+7
source share
1 answer

you're right CompositeCollection has no concept of datacontext , so it cannot inherit it from its parent.

from MSDN:
CompositeCollection can contain items such as strings, objects, XML nodes, elements, as well as other collections. An ItemsControl uses the data in the CompositeCollection to generate its content according to its ItemTemplate. For more information about using ItemsControl objects to bind to collections, see the Binding to Collections section of the Data Binding Overview.

to your question
But how is that working? it sets the source to something, but that something, in this case a CollectionViewSource uses a DataContext (as its not explicitly setting a source).

I think you think that Collection DependecyProperty can bind to any type of IEnumerable , so it doesn't matter how the collection was created while it was created and implemented by IEnumerable .
in your case, CVS inherits the DataContext from the window, and then binds to Items .

with regard to your second example, this does not work, because the ContentPesenter needs a dataContext to work, since it can inherit it, the binding mechanism simply sets itself as a dataContext, even if you tried to bind the Source content to the button, you forgot to set the path, I think, that is why he was ignored. all you have to do to get it working is just to install it like this:

 <ContentPresenter Content="{Binding Source={StaticResource menu}, Path=Content}"/ 
+4
source

All Articles