Answered by:
how to add DataTemplate to .Xaml file dynamically

Question
-
I am using T4 template to generate datatemplate.
My datatemplate file already contains the following root element:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></ResourceDictionary>
Now I am generating following xaml from my T4 template.
<DataTemplate x:Key="PersonItemDetailTemplate" >
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" Height="470" Margin="110,30,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="{Binding ImageUrl}" Width="150" Height="150" Margin="0,0,10,0" VerticalAlignment="Top"/>
</Grid>
</DataTemplate>how can I add this datatemplate to my file?
I tried using XMLDocument but while parsing it throws error that "x: is undefined"..
And I dont want to add the xmlns along with my datatemplate because then it does not work when we see view in design mode... any suggestions?
- Edited by pratikmehta9 Monday, October 21, 2013 8:35 PM
Monday, October 21, 2013 5:33 PM
Answers
-
You could use a ParserContext:
System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext(); parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); System.IO.FileStream stream = new System.IO.FileStream(@"C:\XMLFile1.xml", System.IO.FileMode.Open); DataTemplate dt = System.Windows.Markup.XamlReader.Load(stream, parserContext) as DataTemplate;
XMLFile1.xml:
<DataTemplate x:Key="PersonItemDetailTemplate" > <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" Height="470" Margin="110,30,0,0" > <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Source="{Binding ImageUrl}" Width="150" Height="150" Margin="0,0,10,0" VerticalAlignment="Top"/> </Grid> </DataTemplate>
- Marked as answer by Jamles HezModerator Tuesday, October 29, 2013 8:06 AM
Tuesday, October 22, 2013 6:25 PM
All replies
-
Hi Pratik,
Take a look at the XamlReader class to dynamically load a Xaml file. If you are generating this at runtime you might also consider creating the DataTemplate from code rather than writing Xaml and then parsing it.
--Rob
Tuesday, October 22, 2013 12:52 AMModerator -
Hi Rob,
Thanks for the reply. but Xamlreader also throws error when we dont include namespace..
Tuesday, October 22, 2013 11:06 AM -
You could use a ParserContext:
System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext(); parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); System.IO.FileStream stream = new System.IO.FileStream(@"C:\XMLFile1.xml", System.IO.FileMode.Open); DataTemplate dt = System.Windows.Markup.XamlReader.Load(stream, parserContext) as DataTemplate;
XMLFile1.xml:
<DataTemplate x:Key="PersonItemDetailTemplate" > <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" Height="470" Margin="110,30,0,0" > <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Source="{Binding ImageUrl}" Width="150" Height="150" Margin="0,0,10,0" VerticalAlignment="Top"/> </Grid> </DataTemplate>
- Marked as answer by Jamles HezModerator Tuesday, October 29, 2013 8:06 AM
Tuesday, October 22, 2013 6:25 PM