none
Data binding to dependency property defined in same control. RRS feed

  • Question

  • Hi,

     

    Two controls have a MapProperty defined and im trying to bind the two togethor. The containing control is one of the containing controls.

     

    The code is as such:

     

     

    <UserContol
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Meyertech.Fusion2.GUIFramework"
        x:Class="Meyertech.Fusion2.GUIFramework.MapDocument"
        x:Name="Fusion2DocumentWindow"
        >
      <AdornerDecorator>
        <MapControl x:Name="mapControl" Map="{Binding Path=Map, ElementName=Fusion2DocumentWindow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
      </AdornerDecorator>
    </UserControl>

     

    The output from the debugger states:

     

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Fusion2DocumentWindow'. BindingExpressionStick out tongueath=Map; DataItem=null; target element is 'MapControl' (Name='mapControl'); target property is 'Map' (type 'Map').

     

    Please any insight!

     

    Thanks

    Thursday, November 15, 2007 3:22 PM

Answers

  • try this

     

    <MapControl x:Name="mapControl" Map="{Binding Path=Map, RelativeSource={RelativeSource AncestorType={x:Type UserControl} }, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

     

    Thursday, November 15, 2007 7:16 PM

All replies

  • try using a TemplateBinding (or RelativeSource TemplatedParent)
    ex:
    Code Block

    <MapControl x:Name="mapControl" Map="{TemplateBinding local:MapDocument.Map}" />
      </AdornerDecorator>

    Offcourse, with this form you can't define the updateSourceTrigger.  You could provide these as default values during the construction of the dep prop.
    or:
    Code Block

    <MapControl x:Name="mapControl" Map="{Binding Path=Map, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
      </AdornerDecorator>

    In this form, you can declare custom modes and stuff, but this type of binding is a little bit less efficient than a TemplateBinding .
    Thursday, November 15, 2007 3:59 PM
  • Thanks for the reply.

     

    When using template binding the following exception is thrown:

     

    Cannot convert the value in attribute 'Map' to object of type ''. 

     

    No binding appears to take place when using the relative source method - although no error appears in the output from the debugger.

     

    The dependency property of the user control is set correctly but the value of the bound item is still null. Help!

     

    Thursday, November 15, 2007 4:31 PM
  • try this

     

    <MapControl x:Name="mapControl" Map="{Binding Path=Map, RelativeSource={RelativeSource AncestorType={x:Type UserControl} }, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

     

    Thursday, November 15, 2007 7:16 PM
  • We seem to be missing information.  What is the "source object" for the binding.  We can see that the "target object" is MapControl and it clearly has a "target dependency property" named Map. 

     

    What is the source object that also has a property named Map?

    Where is it in the element tree (in relation to MapControl)?

     

    Is it the MapDocument user control?  If so, the FindAncestor binding that Marlon suggests should work.

    Thursday, November 15, 2007 11:32 PM
  • Thanks this did the trick!

     

    Friday, November 16, 2007 9:50 AM