none
Binding detaching in nested usercontrols with dependency properties RRS feed

  • Question

  • I have two nested User Controls, both with a custom "SelectedItem" DependencyProperty on them.  The deepest user control has a list box bound to a simple collection, the listbox's SelectedItem property is bound to the deepest usercontrol's SelectedItem property.  The usercontrol is inside a second usercontrol, and the first usercontrol's SelectedItem property is bound to the second usercontrol's SelectedItem property.

    So it is like this:
    <window1>
        <usercontrol1 with SelectedItem dependency property>
            <usercontrol2 with SelectedItem dependency property bound to usercontrol1.SelectedItem>
                <listbox with a built-in SelectedItem property bound to usercontrol2.SelectedItem>
            </usercontrol2>
        </usercontrol1>
    </window1>

    When the listbox's SelectedItem changes, it updates usercontrol2's SelectedItem fine.  When usercontrol2's SelectedItem tries to change usercontrol1, the binding breaks.


     


    • Edited by emulamer Tuesday, August 12, 2014 6:18 PM
    Friday, March 27, 2009 10:55 PM

Answers

  • Hello emulamer,

     Please add binding Mode=TwoWay to the UserControl1:
     
       <common:UserControl2 x:Name="whatever" SelectedItem="{Binding ElementName=uc1, Path=SelectedItem, diagnostics:PresentationTraceSources.TraceLevel=High, Mode=TwoWay}" Margin="0,34,0,-34">

     If we bind SelectedItem of UserControl2 to the SelectedItem of UserControl1 by OneWay Mode.

     The initial value of the SelectedItem of UserControl1 is null, So the SelectedItem value of UserControl2 doesn't changed from the UserControl1.

     The value of the SelectedItem of UserControl1 keeps null. The UserControl2 doesn't upgrade the source (UserControl1) in this binding also due to one way binding.

     The value of SelectedItem of UserControl2 changes with the SelectedItem value of ListBox.

    Good luck.


    Please mark the replies as answers if they help and unmark them if they provide no help
    • Edited by Hua Chen Tuesday, March 31, 2009 8:49 AM Edit..
    • Marked as answer by Hua Chen Friday, April 3, 2009 4:56 AM
    Tuesday, March 31, 2009 8:48 AM