如何获取元素WPF的孩子 [英] how to get the children of an element WPF

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

问题描述

我有一个包含一个图像和一个TextBlock一个StackPanel中。一旦双击正在被用户执行我养的一个事件( PS - 。我加入了StackPanel中和它的孩子(图像和编程,如果它的事项文本块)

I have a stackpanel that contains an Image and a TextBlock. I'm raising an event once double click is being performed by the user.(P.S - I'm adding the StackPanel and it's children (the Image and the TextBlock programatically if it matters).

现在,我需要从内部的StackPanel拿到TextBlock元素,我知道我应该使用数据绑定做到这一点,但我是一个初学者WPF,还真没有发现在Web任何关于它的例子。我会很高兴的解释,非常感谢你!

Now, I need to get the TextBlock element from inside the stackpanel, I understand that I should do it using DataBinding, but I'm a beginner to WPF, and really haven't found any examples about it in the web. I'll be glad for an explanation, thank you very much!

(我了解数据绑定前一阵子)。

(I learnt about DataBinding a while ago).

推荐答案

获得某种类型(例如TextBlock的)的第一个子元素的一个简单的方法是这样的:

A simple way of getting the first child element of a certain type (e.g. TextBlock) is this:

var textBlock = panel.Children.OfType<TextBlock>().FirstOrDefault();

您要么得到第一的TextBlock或如果没有任何

You either get the first TextBlock or null if there isn't any.

这篇关于如何获取元素WPF的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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