Get grid.colum of a rectangle
-
24-12-2019 - |
Question
I have a xaml defined like so :
<Grid>
<Rectangle Grid.Column="0" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="1" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="2" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="3" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="4" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="5" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="6" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="7" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="8" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="9" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="10" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Grid.Column="11" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
</Grid>
What I don't find is a way to access the grid column number I,ve tried to cast the rectangle but I don't find the apropriate property of it.
Solution
If you have Rectangle
object you can get it using GetValue() method. Say rect
is object name then you can get it like this:
int column = (int)rect.GetValue(Grid.ColumnProperty);
Grid.Column
is attached property and not normal Dependency Property of Rectangle. Hence you have to pass Grid.ColumnProperty
in there.
OTHER TIPS
Grid.Column
is an attached property and hence defines the static getter and setter methods Grid.GetColumn and Grid.SetColumn. Please pay special attention to the Attached Properties in Code section of the linked MSDN article.
The usual way to get the value of the Grid.Column
property in code is to call the static getter method:
int column = Grid.GetColumn(rect);