How can I refer to an attached property as a data binding source?

Here is a very shortened sample of my code.

<Grid> <Polygon Name="ply" Grid.Column="2" Grid.Row="0" Grid.RowSpan="3" Fill="Orange" Stroke="Orange" Points="0,1 1,3 2,2 2,0 0,0" /> <Line Grid.Column= "{Binding ElementName=ply, Path=Grid.Column, Mode=OneWay}" Grid.Row= "{Binding ElementName=ply, Path=Grid.Row, Mode=OneWay}" Grid.ColumnSpan="{Binding ElementName=ply, Path=Grid.ColumnSpan, Mode=OneWay}" Grid.RowSpan= "{Binding ElementName=ply, Path=Grid.RowSpan, Mode=OneWay}" X1="0" Y1="0" X2="1" Y2="1" /> </Grid> 

The code compiles just fine, without any errors or warnings, but when I run the application, it appears in the output window:

 System.Windows.Data Error: 39 : BindingExpression path error: 'Grid' property not found on 'object' ''Polygon' (Name='ply')'. BindingExpression:Path=Grid.Column; DataItem='Polygon' (Name='ply'); target element is 'Line' (Name=''); target property is 'Column' (type 'Int32') System.Windows.Data Error: 39 : BindingExpression path error: 'Grid' property not found on 'object' ''Polygon' (Name='ply')'. BindingExpression:Path=Grid.Row; DataItem='Polygon' (Name='ply'); target element is 'Line' (Name=''); target property is 'Row' (type 'Int32') System.Windows.Data Error: 39 : BindingExpression path error: 'Grid' property not found on 'object' ''Polygon' (Name='ply')'. BindingExpression:Path=Grid.ColumnSpan; DataItem='Polygon' (Name='ply'); target element is 'Line' (Name=''); target property is 'ColumnSpan' (type 'Int32') System.Windows.Data Error: 39 : BindingExpression path error: 'Grid' property not found on 'object' ''Polygon' (Name='ply')'. BindingExpression:Path=Grid.RowSpan; DataItem='Polygon' (Name='ply'); target element is 'Line' (Name=''); target property is 'RowSpan' (type 'Int32') 

I am clearly not doing this correctly, so my question is:
How to correctly refer to the attached property "Grid.Whatever" on the source element in my data binding? Should I bind in code instead, or is it enough to bind XAML with a different syntax?

+4
source share
1 answer

You are going to laugh, but this is slightly different from the syntax. You just need to put the brackets around the name of the attached property (it took me forever to find out when I did this for the first time):

 <Line Grid.Column= "{Binding ElementName=ply, Path=(Grid.Column), Mode=OneWay}" Grid.Row= "{Binding ElementName=ply, Path=(Grid.Row), Mode=OneWay}" Grid.ColumnSpan="{Binding ElementName=ply, Path=(Grid.ColumnSpan), Mode=OneWay}" Grid.RowSpan= "{Binding ElementName=ply, Path=(Grid.RowSpan), Mode=OneWay}" X1="0" Y1="0" X2="1" Y2="1" /> 

Hope this helps, Anderson

+6
source

All Articles