如何获取 x:Name 值运行时 [英] How to get x:Name value runtime

查看:23
本文介绍了如何获取 x:Name 值运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了:

   <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>FrameworkContentElement,它使用 NameScope 注册元素.不幸的是,您无法检索元素的名称,查找只能以相反的方式进行.

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 值运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆