I have a resource dictionary as follows defined in my UserControl:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../ParameterEditorResourceDictionary.xaml"></ResourceDictionary>
<ResourceDictionary>
<DataTemplate x:Key="ParameterDefault">
<StackPanel Orientation="Horizontal">
<TextBlock Text="("></TextBlock>
<ItemsControl ItemsSource="//I want to set from code">
//some more items here
</ItemsControl>
<TextBlock Text=")"></TextBlock>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Then I have a DataGrid in my UserControl which has a loaded event:
<cc:PEDataGrid AutoGenerateColumns="False"
Loaded="CommonPEGrid_Loaded"
>
</cc:PEDataGrid>
In the loaded event code I have to access the DataTemplate and then traverse to ItemsControl of it and I want to set the ItemsSource property from code. But I am Unable to do it. My code is as follows:::
DataGridTemplateColumn column = null;
int i = 0;
foreach (ParameterLoop obj in tempObj.ParametersAllLoops)
{
column = new DataGridTemplateColumn();
column.Header = "Loop ( " + i.ToString() + " )";
//not working
DataTemplate dt = FindResource("ParameterDefault") as DataTemplate;
Type t = dt.VisualTree.FirstChild.GetType(); //VisualTree always null
//not working
DataTemplate dt = Resources["ParameterDefault"] as DataTemplate;
Type t = dt.VisualTree.FirstChild.GetType(); //VisualTree always null
//I am not able to traverse the visual tree at all !!
//Where am I going worng??
column.CellTemplate = dt;
dg.Columns.Add(column);
i++;
}
I tried to google out, but I am not understanding the solutions given by many!! Please help me regarding this!! A sample illustration would be great.