I'm a little confused about how the data binding works when using these types.
I've read that you can't 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 CompositeCollection has no notion of datacontext and so anything inside of it using a binding has to set the Source property. Such as the following :
<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 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).
So because "list" is declared in the resources of Window, does that mean it gets Windows DataContext? In which case, why doesn't the following also 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>