1

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?

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
  • You can check that [here](https://msdn.microsoft.com/en-us/library/system.windows.controls.columndefinition(v=vs.110).aspx#Properties), answer seems to be: No. But you can probably look it up with something like Sender.Parent.ColumnDefinitions.IndexOf() – H H May 08 '17 at 11:10
  • @Haddi, did you find my solution useful, or do you need some more help? – Massimiliano Kraus May 15 '17 at 10:10
  • Hi, I haven't tried it yet, i'll keep you updated, tnx! – Haddi Goldiner May 17 '17 at 05:33
  • Hi, the events do not work at all... I need to know if the mouse has entered a column definition and which one but i can't find how to do that... – Haddi Goldiner May 17 '17 at 12:50
  • @HaddiGoldiner, I tested my solution and it works, what kind of errors are you encountering? If a `UIElement` covers the whole area of a `ColumnDefinition`, setting the `MouseEnter` on the `ColumnDefinition` or on the `UIElement` is perfectly equivalent. So, if you can't do it on the `ColumnDefinition`, do it on the `UIElement`! – Massimiliano Kraus May 23 '17 at 12:49

1 Answers1

0

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...
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47