如何使用x:Name属性生成映射到元素的基础类中的属性

时间:2015-03-17 23:12:43

标签: c# wpf xaml

我有DataTemplate,后面需要一些代码。此模板包含例如属性为ListBox的{​​{1}}。此模板位于x:Name="myListBox",其中包含属性ResourceDictionary。现在,我如何才能从x:Class="MyNamespace.MyClass"的代码访问MyClass实例?

1 个答案:

答案 0 :(得分:0)

试试这个

ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(myListBox);

DataTemplate yourDataTemplate = contentPresenter.ContentTemplate;

TextBox txtName= yourDataTemplate.FindName("vidList", contentPresenter) 
as TextBox ;
if (txtName!= null)
{
    // Do something with txtNamehere
}