我的用户控件 Layoutroot 上有多个文本块,问题是如何通过名称找到特定的 TextBlock?
谢谢
最佳答案
var myElement =
((FrameworkElement)System.Windows.Application.Current.RootVisual)
.FindName("TextBlockName");
在这种情况下应该可以工作,如果文本块已经被渲染。
为了能够像@ColinE 提到的那样更普遍地轻松遍历可视化树,您还可以使用 Silverlight 工具包。
// requires System.Windows.Controls.Toolkit.dll
using System.Windows.Controls.Primitives;
var myElement = myRoot.GetVisualDescendants().OfType<TextBlock>()
.Where(txb => txb.Name == "TextBlockName").FirstOrDefault();
关于silverlight - 在 Silverlight 中从 LayoutRoot 中查找控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5076520/