Since you apparently can't create a Silverlight DataTemplate in C#, I'm trying to create one in XAML. I have a converter that I need to refer to, that I have defined in C# in another namespace. I've tried doing this:
<UserControl.Resources>
<DataTemplate x:Key="PriceTemplate">
<TextBlock Text="{Binding Price, Converter={Converters:PriceConverter}}" />
</DataTemplate>
</UserControl.Resources>
Where Converters is an xmlns that points to the correct namespace. However, I get a compilation error that says:
Type 'Converters:PriceConverter' is used like a markup extension but does not derive from MarkupExtension.
I tried adding System.Windows.Markup.MarkupExtension as a parent to my converter, but it apparently doesn't exist in Silverlight.
How can I refer to my converter in XAML, without having to rewrite it in XAML?
You want to make a static resource first, then bind to the converter that is a static resource.
<UserControl.Resources>
<conv:IntConverter x:Key="IntConverter"></conv:IntConverter>
</UserControl.Resources>
<StackPanel>
<TextBlock x:Name="Result" Margin="15" FontSize="20"
HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{Binding Converter={StaticResource IntConverter}}">
</TextBlock>
</StackPanel>
</Window>
So the "conv:" xml namespace was registered at the top of the document like you do with custom controls:
xmlns:conv="clr-namespace:MyFooCompany.Converters"
This example is adapted from the below linked tutorial regarding the same issue for WPF:
http://www.dev102.com/2008/07/17/wpf-binding-converter-best-practices/