Search code examples
c#wpfwpfdatagrid

Getting grid.column property of a control in a general styled event handler


i have a lot of text boxes in my app, and a style which sets the event handler:

<EventSetter Event="MouseEnter" Handler="GeneralTextBoxMouseEnter"/> 

Text Boxes are Located in Grids so for example this is the xaml code for one of the text boxes:

<Grid>
    <TextBox Name="sat6" Grid.Column="1" Style="{StaticResource anHourSatAm}" />
</Grid>

this is the GeneralTextBoxMouseEnter event handler

    private void GeneralTextBoxMouseEnter(object sender, MouseEventArgs e)
    {
        TextBox tb = (TextBox)sender;

        MessageBox.Show((String)(tb.Grid.Column);

    }

i get an error that such a property doesn't exist. but in properties box of VS2010 it exists, how can i retrieve the value?


Solution

  • you need to use static method named GetColumn of Grid.

    private void GeneralTextBoxMouseEnter(object sender, MouseEventArgs e)
        {
            TextBox tb = (TextBox)sender;
    
            MessageBox.Show(Grid.GetColumn(tb));
    
        }
    

    hope it helps..