I'm trying to figure out if a ColumnDefinition
knows it's position inside the Grid
or not.
I have 3 columns as follows:
<Grid.ColumnDefinitions>
<ColumnDefinition Name="Left" Width="120" MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave"/>
<ColumnDefinition/>
<ColumnDefinition Width="120" MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave"/>
</Grid.ColumnDefinitions>
<view:Button Grid.Column="0" x:Name="GoLeft" Visibility="Hidden"/>
<view:MarkBox Grid.Column="1" x:Name="MarkBox1"
MinHeight="110" MinWidth="100" Width="100" Height="110"/>
<view:Button Grid.Column="2" x:Name="GoRight" Visibility="Hidden"/>
And I want to apply the same MouseEnter
function on both, put perform different actions if it's for the left or right ColumnDefinition
. Does ColumnDefinition
have a property containing its number or position inside the Grid
?
Mouse events on GridColumn
are not working for me.
But every UIElement
child of the Grid
can have a Mouse event handler. For example:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="Red" MouseEnter="OnMouseEnter"/>
<Grid Grid.Column="1" Background="Green" MouseEnter="OnMouseEnter"/>
<Rectangle Grid.Column="2" Fill="Blue" MouseEnter="OnMouseEnter"/>
</Grid>
As you can see, I attached the same event handler for the MouseEnter
event on different controls: a Border
, a Grid
, a Rectangle
... Every control has a position relative to the parent Grid
( Grid.Row
, Grid.Column
), so in the event handler you can check the AttachedProperty of the sender
:
private void OnMouseEnter(object sender, MouseEventArgs e)
{
var element = sender as UIElement;
var columnIndex = element.GetValue(Grid.ColumnProperty);
// use columnIndex somehow...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.