I created a UserControl which has a ComboBox within it that I would like to expose so I can bind to the ComboBox from within the MainWindow.xaml when I add the UserControl. I found the below code, which works, but I don't understand why/what is actually going on.
<ComboBox HorizontalAlignment="Left" Margin="38,2,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Views:UserControl1, AncestorLevel=1}, Path=ComboBox1ItemsSource}" />
**(Views) is a XAML defined namespace to my Views directory.
I would've though I could have just set the ItemsSource to:
ItemsSource = "{Binding RelativeSource={RelativeSource Self}, Path=ComboBox1ItemsSource}"
Why wouldn't setting the source to Self work? What is going on when the using the FindAcestor as shown in the first bit of code above.
Also in case it is important, I did have to add the following code behind for the first bit of code to work.
public static readonly DependencyProperty ComboBox1ItemsSourceProperty;
public IEnumerable ComboBox1ItemsSource { get; set; }
static UserControl1()
{
ComboBox1ItemsSourceProperty = DependencyProperty.Register("ComboBox1ItemsSource", typeof(IEnumerable), typeof(UserControl1));
}