How to get x:Name value runtime

后端 未结 2 1314
天命终不由人
天命终不由人 2021-01-15 16:00

I got:

   
      
          

        
相关标签:
2条回答
  • 2021-01-15 16:30

    When you use the x:Name syntax on an element that isn't a FrameworkElement or FrameworkContentElement, it registers the element with a NameScope. Unfortunately, you can't retrieve the name for an element, the lookup only works the other way around.

    If you need to pass additional information about the DataGridColumn, a custom attached property would be an easy way of doing it.

    More information about x:Name can be found on MSDN. Also, the NameScope documentation describes its behavior.

    0 讨论(0)
  • 2021-01-15 16:44

    You can leverage reflection to grab the value with:

    GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
    String xName = (String)headerClicked.getType().GetProperty("Name").GetValue(headerClicked, null);
    
    0 讨论(0)
提交回复
热议问题