Mizipzor
Mizipzor

Reputation: 52321

WPF bind property to Dependency Property in child control

Ive created a custom UserControl that contains a single combobox. The currently selected value in the combobox is bound to the custom UserControls Dependency Property.

XAML:

<UserControl>
    <ComboBox
        ItemsSource="{Binding AllEntries}"
        SelectedItem="{Binding SelectedEntry}" />
</UserControl>

Code behind:

public partial class MyCombobox : UserControl
{
    public static DependencyProperty SelectedEntryProperty =
        DependencyProperty.Register("SelectedEntry",
            typeof(ComboboxEntry),
            typeof(MyCombobox));

    public ComboboxEntry SelectedEntry
    {
        get { return (ComboboxEntry)GetValue(SelectedEntryProperty); }
        set { SetValue(SelectedEntryProperty, value); }
    }
}

Now the problem is that another component includes this extended combobox control. In the containing control I want to run some logic when the user selects a new value in the combobox. Im a bit lost as to how I set up that hook. Must MyCombobox expose a custom event which is fired from a PropertyChanged callback in the SelectedEntry Dependency Property? Seems hacky but I cant figure out another way.

Upvotes: 0

Views: 4446

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178630

Why not just use another binding?

<OuterControl>
    <StackPanel>
        <local:MyCombobox x:Name="myComboBox"/>
        <TextBlock Text="{Binding SelectedEntry, ElementName=myComboBox}"/>
    </StackPanel>
</OuterControl>

Upvotes: 2

Related Questions