2009-09-11 45 views
4

下面是我的代碼的一個簡短的例子。如何才能將附加屬性引用爲數據綁定的來源?

<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> 

代碼編譯就好了,沒有任何錯誤或警告 - 但是當我運行應用程序,這將出現在輸出窗口:

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') 

我顯然不是正確地做這個,所以我的問題是:
如何正確引用數據綁定中源元素上的附加屬性「Grid.Whatever」?我將不得不在代碼隱藏中執行綁定,或者使用不同語法的XAML綁定是否足夠?

回答

6

你會笑,但它只是稍微不同的語法。你只需要把括號圍繞附加屬性的名稱(這花了我永遠找出我第一次做到了):

<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" 
/> 

希望這有助於 安德森

+1

完美的作品。是的,我笑了。當如此簡單和愚蠢的行爲阻礙了你一個小時的進步時,這太令人沮喪了。 – Giffyguy 2009-09-11 04:03:52

+1

很高興我可以幫助**和**發笑。 – 2009-09-11 04:08:50

+1

@Giffyguy只需一個小時? – 2013-07-18 15:25:03