views:

292

answers:

1

I try to refactor such XAML by introducing new user control:

<Window ...>
  <ComboBox ItemsSource="{Binding Greetings}" />
</Window>

After adding a control I have

ControlA XAML:

<UserControl ...>
  <ComboBox ItemsSource="{Binding Items}" />
</UserControl>

ControlA C#:

public static readonly DependencyProperty ItemsProperty =
  WpfUtils.Property<IEnumerable, ControlA>("Items");

public IEnumerable Items { get; set; }

New Window XAML:

<Window ...>
  <uc:ControlA Items="{Binding Greetings}" />
</Window>

After this I see nothing in ComboBox. What is wrong here?

A: 

Your ComboBox is binding to the DataContext. Since your DataContext is still an object with a list called Greetings, this will not work...

Your ContolA should resemble something like this:

<UserControl x:Name="Root" ...>
  <ComboBox ItemsSource="{Binding ElementName=Root, Path=Items}" />
</UserControl>

Now, your combobox binds to the Items property of your ControlA, instead of your DataContext property...

Hope this helps..

Arcturus
I had this.DataContext = this; in controls code-behind. Also added Root and new ComboBox as you suggested, but still get empty boxes.
alex2k8
Gotcha, setting 'this.DataContext = this;' overwrote the context for main Window.
alex2k8