如何引用附加属性作为数据绑定的来源?

时间:2021-07-09 06:03:30

Here's a much-abbreviated 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, this 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'm clearly not doing this correctly, so my question is:
How do I properly reference the attached property "Grid.Whatever" on the source element in my data-binding? Am I going to have to perform the binding in the code-behind instead, or will a XAML binding with different syntax be sufficient?

我显然没有正确地做到这一点,所以我的问题是:如何正确引用数据绑定中源元素的附加属性“Grid.Whatever”?我是否必须在代码隐藏中执行绑定,或者使用不同语法的XAML绑定是否足够?

1 个解决方案

#1


6  

You are going to laugh, but it's just slightly different syntax. You just need to put parenthesis around the attached property name (this took me forever to figure out the first time I did it):

你会笑,但语法略有不同。你只需要在附加的属性名称周围添加括号(这让我永远想知道我第一次这样做了):

<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

希望这会有所帮助,安德森

#1


6  

You are going to laugh, but it's just slightly different syntax. You just need to put parenthesis around the attached property name (this took me forever to figure out the first time I did it):

你会笑,但语法略有不同。你只需要在附加的属性名称周围添加括号(这让我永远想知道我第一次这样做了):

<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

希望这会有所帮助,安德森