有没有办法通过 Tag 属性获取 XAML 元素? [英] Is there any way to get XAML element by Tag property?

查看:35
本文介绍了有没有办法通过 Tag 属性获取 XAML 元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Is there any way to get the XAML element by its tag value??

我的代码是这样的:

<Grid Tap="StackPanel_Tap" Tag="{Binding Type}" >
       <Border BorderThickness="0"  CornerRadius="0"  BorderBrush="White" Width="100" Height="100"  HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="1" >
      <Image Tag="{Binding Type}" Source="{Binding Location}" Opacity="1" Width="100"  Height="100" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center" UseLayoutRounding="True"  >
       </Image>
      </Border>
</Grid>

我正在为 Grid 绑定 Tag 属性,有没有办法通过使用 Tag 属性来获取 Grid 的对象??

I am binding Tag Property for Grid , is there any way to get an object of Grid by using Tag Propertty??

抱歉,网格在 ListBox 中的信息较少.如何访问网格对象?

Sorry for less information the thing is grid is in ListBox .. How can i Access The Grid Object??

感谢和问候夜叉

推荐答案

创建一个递归方法,该方法使用 System.Windows.Media.VisualTreeHelper 获取页面的所有子级.对于每个子项,测试它是否属于 Grid 类型,以及它是否具有与您要查找的内容相同的标签.

Create a recursive method which uses System.Windows.Media.VisualTreeHelper to get all children of the page. For each child test if it's of type Grid and if it has a Tag equal to whatever you're looking for.

这篇关于有没有办法通过 Tag 属性获取 XAML 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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