views:

151

answers:

1

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.

A: 

Why not just use another binding?

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

HTH,
Kent

Kent Boogaart
I was about to come back and delete the question (it didnt get any answers during the first hour I checked)... but you're right, another binding was what I was trying to do, but Im doing way to many ugly hacks with the DataContext property in the code behind so I had screwed up said binding. Its working properly now, using the bind-through-elementname approach you described. Im gonna accept and leave it here, some rep for you but I doubt it will help anyone else that stumbles here. Cheers! :)
mizipzor