如何从数据模板中提取UI元素?

时间:2013-01-30 16:37:55

标签: c# wpf datatemplate frameworkelementfactory

我使用以下代码创建数据模板项:

 FrameworkElementFactory textCtrl = new FrameworkElementFactory(typeof(TextBox));            
        Binding binding1 = new Binding();
        string path = "Syncfusion";
        binding1.Path = new PropertyPath(path);
        textCtrl.SetValue(TextBox.TextProperty,binding1);
        Binding binding2 = new Binding();
        string path2 = "Text1";
        binding2.Path = new PropertyPath(path2);
        textCtrl.SetValue(TextBox.NameProperty, binding2);
        DataTemplate dt = new DataTemplate();
        dt.VisualTree = textCtrl;

如何从数据模板中获取文本框..?

我尝试了以下链接 Link 1

Link 2

但我没有把事情弄错......

我在xaml中使用了以下代码

<DataTemplate>
      <TextBlock Text="{Binding CellBoundValue}" gridcommon:VisualContainer.WantsMouseInput="False"/>
 </DataTemplate>

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

据我所知,微软不建议使用FrameworkElementFactory,它可能会被弃用一段时间(不确定)。

但是如果你想要这样做,你必须应用你的DataTemplate来创建DataTemplate中声明的控件实例。例如,您可以使用ContentControl或ItemsControl来执行此操作。

答案 1 :(得分:1)

此链接可以为您提供帮助:Creating WPF Data Templates in Code The Right Way