I want to define the ItemsSource
of an ItemsControl
in XAML and in those items use data binding to the DataContext
of my view, but I cannot get it to work.
Here is what a simplified version would look like:
<ComboBox DisplayMemberPath="Label" DataContext="Item 3">
<ComboBox.ItemsSource>
<x:Array Type="{x:Type local:Item}">
<local:Item Label="Item 1" />
<local:Item Label="{Binding}" />
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
with the Item
defined like this:
public class Item : DependencyObject
{
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register(nameof(Label), typeof(string), typeof(Item));
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
}
I am expecting to see two items in the ComboBox with "Item 1" and "Item 2" as text, but the second item has a blank text. Why?