locked
Binding to an attached property in code RRS feed

  • Question

  • I'm trying to bind to an attached property in code (not XAML) and I can't seem to get the right syntax. I think the problem relates to namespaces, but I'm not sure what the right way to fix the problem is.

    For example, in XAML I can do this (note: InkCanvas.Top is not the property I'm trying to use, this is an example):

    <TextBlock Text="{Binding ElementName=button1, Path=(InkCanvas.Left)}" Name="bound"/>

    And the equivalent in code would be this:

                Binding b = new Binding("(InkCanvas.Left)");
                b.ElementName = "button1";
                bound.SetBinding(TextBlock.TextProperty, b);

    And both work fine. However, in my situation I am binding to an attached property from another namespace, so in XAML I can do this (assuming I have the namespace definition in XAML for cont):

    <TextBlock Text="{Binding ElementName=button1, Path=(cont:MyControl.MyProperty)}" Name="bound"/>

    But in code, this doesn't seem to work:

                Binding b = new Binding("(cont:MyControl.MyProperty)");
                b.ElementName = "button1";
                bound2.SetBinding(TextBlock.TextProperty, b);

    So what's the correct way to tell the binding that the property it should be binding to is defined in another namespace?
    Wednesday, May 6, 2009 7:37 PM

Answers

  • Nevermind, I think I figured it out. I needed to do this:

                Binding b = new Binding();
                b.Path = new PropertyPath(MyControl.MyProperty);
                b.ElementName = "button1";
                bound2.SetBinding(TextBlock.TextProperty, b);
    • Marked as answer by wjousts Wednesday, May 6, 2009 8:00 PM
    Wednesday, May 6, 2009 8:00 PM