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

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

问题描述

 < ListView.View GridViewColumnHeader.Click =ColumnHeaderClick> 
< GridView>
< GridViewColumn x:Name =ColHeader =Item>



private void ColumnHeaderClick(object sender,RoutedEventArgs e)
{
GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;


$ / code>

现在,如何获取GridViewColumn的x:Name值在方法中?我无法在xaml中为列添加'Name'属性,并且它在运行时像空字符串一样。虽然我能够获得标题,但仍需要获取x:Name值。 当您在元素上使用 x:Name 不是 FrameworkElement FrameworkContentElement ,它用 NameScope 。不幸的是,你不能检索元素的名称,查找只能用其他方法。



如果您需要传递有关DataGridColumn的其他信息,附加属性将是一个简单的方法。



有关 x:可以在MSDN上找到名称。另外, NameScope文档描述了它的行为


I got:

   <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;

     }

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.

解决方案

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.

这篇关于如何获得x:名称值运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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