Similar to another question, I would like to access the x:Name property of an object through code, tough in this case the object in question is not a FrameworkElement
and thus does not have a Name property. I do not have access to the member variable either.
In my situation, I have a ListView
with named columns and would like to extend the ListView class so that it persists the column layout. For this functionality I need named columns, and it would make sense to me to re-use the x:Name property I need to set anyway for other reasons, instead of adding an attached "ColumnName" property for example.
My current "solution":
<GridViewColumn Header="X" localControls:ExtendedListView.ColumnName="iconColumn" />
Desired:
<GridViewColumn Header="X" x:Name="iconColumn" />
So is it possible to get the "x:Name" value somehow?
Best Answer
See the answers by IanG in the following thread:
How to read the x:Name property from XAML code in the code-behind
So, all x:Name does is that it gives you access to this object by creating a field with that specific name. If you want to get the x:Name of it, you'll have to iterate all your fields and see if the field is the object you're looking for, and in that case, return the field name.
He does present a method to do this in the code behind file, although I think your current approach with an attached property is a much better solution