问题描述
我得到了:
<ListView.View GridViewColumnHeader.Click="ColumnHeaderClick">
<GridView>
<GridViewColumn x:Name="Col" Header="Item">
private void ColumnHeaderClick(object sender, RoutedEventArgs e)
{
GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
}
现在,如何获取方法中GridViewColumn的x:Name值?我不能在 xaml 中为列添加名称"属性,它在运行时就像空字符串一样.虽然我能够获取标题,但仍然需要获取 x:Name 值.
Now, how to get the x:Name value of GridViewColumn in the method? I can't put 'Name' property for the column in xaml, and it comes like empty string in runtime. Although I am able to get the header, still need to get x:Name value.
推荐答案
当您在不是 FrameworkElement
或 的元素上使用
,它使用 x:Name
语法时code>FrameworkContentElementNameScope
注册元素.不幸的是,您无法检索元素的名称,查找只能以相反的方式进行.
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.
如果您需要传递有关 DataGridColumn 的其他信息,自定义附加属性将是一种简单的方法.
If you need to pass additional information about the DataGridColumn, a custom attached property would be an easy way of doing it.
有关 x:Name 的更多信息可在 MSDN 上找到.此外,NameScope 文档描述了其行为.
这篇关于如何获取 x:Name 值运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!