locked
Binding to an own attached property RRS feed

  • Question

  • Hello,

     

    i have a litte problem binding to an attached property declared in my user control.

    To use an attached property as the path of a binding i have to write something like

    Code Snippet
    Text="{Binding Path=(DockPanel.Dock) ...}"

     After starting the application i can see the text "Left", since the property for the control is set to "Left".

     

    If i now use my own attached property

    Code Snippet
    Text="{Binding Path=(local:DemoControl.DemoProperty) ...}"

     i get an binding path error showing me, that he can't find the property.

     

    I have used snoop to ensure, that the property is registered. I also can use this property in my style and trigger. Therefore i think the property itself is correct.

     

    Is there another syntax for self declared attached properties?

     

    Thanks for any hints

    Tuesday, June 19, 2007 11:12 AM

Answers

  • Hello,

     

    thanks for your response.

    Yes my code is quite similar. But i found the problem with the help of your code.

     

    I usually use the binding syntax for lazy people. As far as i know it is possible to omit the name of the path parameter in the binding markup.

     

    Code Snippet

    <TextBlock Text="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"/>
     <TextBlock Text="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}"/>

     

    Both TextBlock's have the same text.

    The same is true for attached properties. 

    Code Snippet

    <TextBlock DockPanel.Dock="Right" Text="{Binding (DockPanel.Dock), RelativeSource={RelativeSource Self}}"/>
     <TextBlock DockPanel.Dock="Right" Text="{Binding Path=(DockPanel.Dock), RelativeSource={RelativeSource Self}}"/>

     

    Again, both TextBlocks have the same text.

     

    But if i use an attached property, declared in my own namespace i have to write

     

    Code Snippet

    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding Path=(local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>

     

    If i just use the short version (without the name of the path parameter)

     

    Code Snippet

    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding (local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>

     

    i get a binding error ['(localBig SmileemoControl.DemoProperty)' property not found on 'object' ''TextBlock']. But the property is attached to this TextBlock. I can verify that with snoop.

     

    But that is not enough. If i use two TextBlock's

     

    Code Snippet
    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding Path=(local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>
    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding (local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>

     

    i get no error for the second TextBlock and both have the same text.

     

    Thats a little bit strange, but it works and that is important.

     

    Thanks

    Maik

     

     

    Tuesday, June 26, 2007 7:50 AM

All replies

  • Has nobody noticed this before?
    Monday, June 25, 2007 3:06 PM
  • I was able to get this to work with this property definition:

     

    Code Snippet

    public class DemoControl : Control

    {

     

        public static int GetDemoProperty(DependencyObject obj)

        {

            return (int)obj.GetValue(DemoPropertyProperty);

        }

        public static void SetDemoProperty(DependencyObject obj, int value)

        {

            obj.SetValue(DemoPropertyProperty, value);

        }

     

        public static readonly DependencyProperty DemoPropertyProperty =

            DependencyProperty.RegisterAttached("DemoProperty",

                                                typeof(int),

                                                typeof(DemoControl) );

     

     

    }

     

     and this markup:

     

    Code Snippet

    <TextBlock

        Name='TextBlock1'

        local:DemoControl.DemoProperty='1'

        Text='{Binding ElementName=TextBlock1,

                       Path=(local:DemoControl.DemoProperty) }' />

     

     

    Does yours look similar?

    Monday, June 25, 2007 5:52 PM
  • Hello,

     

    thanks for your response.

    Yes my code is quite similar. But i found the problem with the help of your code.

     

    I usually use the binding syntax for lazy people. As far as i know it is possible to omit the name of the path parameter in the binding markup.

     

    Code Snippet

    <TextBlock Text="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"/>
     <TextBlock Text="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}"/>

     

    Both TextBlock's have the same text.

    The same is true for attached properties. 

    Code Snippet

    <TextBlock DockPanel.Dock="Right" Text="{Binding (DockPanel.Dock), RelativeSource={RelativeSource Self}}"/>
     <TextBlock DockPanel.Dock="Right" Text="{Binding Path=(DockPanel.Dock), RelativeSource={RelativeSource Self}}"/>

     

    Again, both TextBlocks have the same text.

     

    But if i use an attached property, declared in my own namespace i have to write

     

    Code Snippet

    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding Path=(local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>

     

    If i just use the short version (without the name of the path parameter)

     

    Code Snippet

    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding (local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>

     

    i get a binding error ['(localBig SmileemoControl.DemoProperty)' property not found on 'object' ''TextBlock']. But the property is attached to this TextBlock. I can verify that with snoop.

     

    But that is not enough. If i use two TextBlock's

     

    Code Snippet
    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding Path=(local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>
    <TextBlock local:DemoControl.DemoProperty="true" Text="{Binding (local:DemoControl.DemoProperty), RelativeSource={RelativeSource Self}}"/>

     

    i get no error for the second TextBlock and both have the same text.

     

    Thats a little bit strange, but it works and that is important.

     

    Thanks

    Maik

     

     

    Tuesday, June 26, 2007 7:50 AM
  • Thanks for reporting this issue.

    A workaround that should work for this...make sure that you use "local:" somewhere in a tag name or setting a property.  (not just inside of some value that gets typeconverted).

     

    PropertyPath uses IXamlTypeResolver...which appears to be missing namespaces that weren't considered used in the xaml file...

     

    Thanks, Rob

    Rob Relyea | Program Manager, WPF & Xaml Language Team

    robrelyea.com | /blog | /wpf | /xaml

     

    Tuesday, July 10, 2007 12:53 AM