Reputation: 2834
I'm trying to kick out a DataTemplate at runtime for the ListBox's ContentTemplateSelected. The data types and values are not known at runtime. Here's an example of one. When I call this:
var dataTemplate = (DataTemplate)XamlReader.Load(xaml.ToString());
on the generated xaml:
<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:DSI.MyProject.Converters;assembly=DSI.MyProject"
>
<DataTemplate.Resources>
<converters:StringValueToBoolConverter x:Key="stringValueToBoolConverter" />
</DataTemplate.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="128" />
</Grid.ColumnDefinitions>
<Border BorderThickness="1" BorderBrush="Black" Grid.Column="0" Background="{StaticResource PhoneContrastBackgroundBrush}" >
<CheckBox IsChecked="{Binding [CheckEd], Mode=TwoWay}" Padding="2" HorizontalAlignment="Left" />
</Border>
</Grid>
</DataTemplate>
I get the error:
The attachable property Resources was not found in type DataTemplate
Am I missing something in the includes?
Even though this example doesn't use it, the converter will be used in the IsChecked binding.
It seems to work for this person: Dynamically added DataTemplate - StaticResource for Converter can't be found
My original issue: WPF using converter in datatemplate created from xaml text and XamlReader on phone 8
Upvotes: 0
Views: 482
Reputation: 4992
I think you are confusing WPF behavior with the SilverLight XAML distribution for Windows and Windows Phone 8. Unfortunately, Resources is a property of the FrameworkElement base class, while DataTemplate derives from the lower level DependencyObject class. As such, it does not support a resource collection.
As a fix, consider making the converter part of your Application or Page resources rather than part of the DataTemplate directly.
Upvotes: 1