Get column name from (object sender, RoutedEventArgs e)

1.3k views Asked by At

I would like to get the column name of the cell the same way i did with its content on my loose focus method.I can get the content but not the column header.

private void lostFocus(object sender, RoutedEventArgs e)
{
    var jj = sender as DataGridColumnHeader;          
    var box = sender as TextBox;          

    if (box != null && box.Text != "0")
    {
        var ff =  jj.Column.Header.ToString();          
        if (ff == "column1") { amount1 = Int32.Parse(box.Text); }
        if (ff == "column2") { amount2 = Int32.Parse(box.Text); }
        if (ff == "column3") {amount3 = Int32.Parse(box.Text); }
    }
    else
    {

    }
}

xaml code

<toolkit:DataGridTemplateColumn Header="column1" Width="8*">
  <toolkit:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBox Padding="0" LostFocus="OnGotFocus" GotFocus="OnGotFocus" />
    </DataTemplate>
  </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

2

There are 2 answers

0
kay-B On BEST ANSWER

Updated Xamal... set the textbox name to be the same as header name

<toolkit:DataGridTemplateColumn Header="column1" Width="8*">
  <toolkit:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBox Padding="0" Name="column1" LostFocus="OnGotFocus" />
    </DataTemplate>
  </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

then just got the name from my sender.... simplicity works

private void lostFocus(object sender, RoutedEventArgs e)
{      
    var box = sender as TextBox;          
    if (box != null && box.Text != "0")
    {
        var name = box.Name.ToString();

        if (name == "column1") { amount1 = Int32.Parse(box.Text); }
        if (name == "column2") { amount2 = Int32.Parse(box.Text); }
        if (name == "column3") {amount3 = Int32.Parse(box.Text); }
    }
    else
    {

    }
}

Thanks for the help https://stackoverflow.com/users/2047469/olaru-mircea

3
Olaru Mircea On

Getting column header

What you need is already provided here by Fredrik. Basically you need to get all the children of type DataGridColumnHeader existing in the DataGrid. Check the column reference and then get the header.

Further more, i see you are getting the DataGridColumnHeader from sender. In order to reach the DataGrid object you can use a helper method :

    public static T FindParent<T>(DependencyObject child) where T : DependencyObject
    {
        //get parent item
        DependencyObject parentObject = VisualTreeHelper.GetParent(child);

        //we've reached the end of the tree
        if (parentObject == null) return null;

        //check if the parent matches the type we're looking for
        T parent = parentObject as T;
        if (parent != null)
            return parent;
        else
            return FindParent<T>(parentObject);
    }

Use it like this:

DataGrid parentGrid = FindParent<DataGrid>(sender as DataGridColumnHeader );

or starting from TextBox

DataGrid parentGrid = FindParent<DataGrid>(sender as TextBox);

I am not exactly sure about your scenario.