c# - 检查 Canvas Child 是否存在 : by tag

标签 c# wpf wpf-controls

如果 Canvas 子项存在,我如何通过标签检查?

例如,我想知道在我的 Canvas (canvas1) 中是否存在一个带有标签“p”的 child 。

最佳答案

How can I can check by tag if a canvas child exist?

所以我想你想要的是 bool 值

bool result = mycanvas.Children.Cast<FrameworkElement>()
                      .Any(x => x.Tag != null && x.Tag.ToString() == "p");

我使用 (x.Tag == null ? "": x.Tag.ToString()) == "p") 只是为了确保标签属性不为空。

如果你想要这个元素

FrameworkElement result = mycanvas.Children.Cast<FrameworkElement>()
                                           .Where(x => x.Tag != null && 
                                                  x.Tag.ToString() == "p")
                                           .First();

关于c# - 检查 Canvas Child 是否存在 : by tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10833719/

相关文章:

wpf - 如何让箭头指向我的边框?

c# - 检查 RoutedEvent 是否有任何处理程序

c# - 如何获取 DependencyProperty 的默认值

wpf - itemspresenter、contentpresenter、itemscontrol、contentcontrol 之间的区别

c# - ASP.NET 在参数 ControlParameter 中找不到对象

c# - 在不使用 $type 的情况下在 JSON.NET 中序列化字节数组

c# - 从不同的线程调试

wpf - 以编程方式选择 WPF ListView 中的项目

c# - 64 位 .Net 应用程序中的内存限制?

c# - EF6 DbSet<T> 在 Moq 中返回 null