DataGrid- retrieving a Column's Name and or Tag property

Apr 21, 2009 at 7:57 PM
Edited Apr 21, 2009 at 8:10 PM
Hi,

I need to identify a column by name or tag in a

wpftk

 

:DataGridTextColumn

 

 

 

 

 

 

 

 

 

in the CodeBehind. Is there anyway to do this?    I set x:Name (or Name), which returns "" and Tag which returns NULL.

Is there any way to get the Name of the Column? I don't want to use the Header because in my case, I want to be able to replace the Header description at runtime based on some user settings.

Thanks,
Chris

Coordinator
Apr 21, 2009 at 8:20 PM
Hi Chris,

If you use x:Name you will be able to access the instance of the column by name in the code-behind (e.g. if you use x:Name="myColumn" in your xaml you can set myColumn.Width in your code-behind). 

If this doesn't cover your scenario let us know.

Thanks,
Alexis
Apr 21, 2009 at 8:29 PM

Hi Alexis,

Thanks, that definitely helps, but I've still got a problem.

My scenario is that I've got a user clicking on a cell within a grid. I need to know which column they clicked on, so that, based on which column it is, I have to display some help text about it in another textbox on the page.

So what I need is a way to retrieve the Name of the column within the GotFocusEvent event on the DataGrid itself. I can get the result this way by using your example above:

((DataGridCell)e.Source).Column = myColumn

I could use this, but then I'd have to have an if/else for each column in the grid, doing the comparison of the source column to each.

Is there a way to get the x:Name value that I'm setting on the column from a 

 

RoutedEventArgs?

Thanks,
Chris

Coordinator
Apr 21, 2009 at 8:50 PM
For a DataGridColumn there is no runtime concept of a name; you can only retrieve the column instances you've created.  In your case you probably want to create an attached property (say "ColumnIdentifier") and set it on each column to some identifying tag.

Does that help?

Alexis
Apr 21, 2009 at 8:58 PM
Hi Alexis,

I think that will help. Do you have an example of an attached property on a datagrid column like that?

Thanks,
Chris