R – How to access x:Name from Code Behind

datagridwpf

I have a datagrid in which I am using DataGridTemplateColumn and DataGridTextColumn.
I want to access these columns at runtime so I have assigned x:Name property to them.
But I was not getting that value in code behind, so I looked for DataGrid and then read objects by iterating through DataGrid.Columns. How can I read x:Name property from that object in C#?

I need this to perform some specific operations with particular columns at runtime.

Best Answer

The datagrid column does not get added to the visual tree. (so maybe you cant access it in the code behind because of this) - see vinces blog on the visual layout.

You can look at the header property, or you could derive and add your own property to uniquely identify the column. That's what I do, I've found the columns a bit vanilla and have derived a fair few for different uses.

Related Topic