I am having problem putting data into controls on wpf TabItem. I have defined several DataTemplates in xaml. Here is one of them:
<Window.Resources>
...
<DataTemplate x:Key="memoTab">
<TextBox Name="memoTextBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AcceptsReturn="True" />
</DataTemplate>
...
</Window.Resources>
I then create new tab in code behind as follows:
TabItem tab = new TabItem();
tab.Header = "Memo";
tab.ContentTemplate = (DataTemplate)FindResource("memoTab");
tab.ApplyTemplate();
System.Windows.Controls.TextBox tb = (System.Windows.Controls.TextBox)tab.Template.FindName("memoTextBox", tab);
if (tb != null) tb.DataContext = memo; //string memo is created earlier as linq query
tabControl.Items.Add(tab); //tabControl is xaml defined
The problem is that tb is always null, and therefore no data appears in the text box (the text box itself shows up in the tab and it is functional)
I do not use xaml to create tabs in tabControl (except the first one, which is not using DataTemplate and is fine) because they are meant to be added and removed by the user at run time.
Any ideas?