I am looking for an option to build a datatemplate in C # code. I used:
DataTemplate dt = new DataTemplate(typeof(TextBox)); Binding bind = new Binding(); bind.Path = new PropertyPath("Text"); bind.Mode = BindingMode.TwoWay; FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBox)); txtElement.SetBinding(TextBox.TextProperty, bind); txtElement.SetValue(TextBox.TextProperty, "test"); dt.VisualTree = txtElement; textBox1.Resources.Add(dt, null);
But it does not work (it is placed in the Loaded-Method window - therefore, the word "test" should appear in my text box when the window starts). Any idea?
source share