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/
You seem to be confusing Types with Instances. A converter type will exist "in" a namespace however in binding we do not specify a type as the converter. Instead we give the binding an actual instance of that type.
Generally IValueConverter
instances are stateless, hence we can hold a common instance anywhere in a the chain of resource dictionaries available where the instance of a DataTemplate is loaded.
In xaml we can reference another namespace by creating a new alias to cover it. With that in mind your xaml could look something like this:-
<UserControl x:Class="SilverlightApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication1"
xmlns:localConverters="clr-namespace:SilverlightApplication1.Converters">
<UserControl.Resources>
<localConverters:PriceConverter x:Key="PriceConverter" />
<DataTemplate x:Key="Test">
<TextBlock Text="{Binding Price, Converter={StaticResource PriceConverter}}" />
</DataTemplate>
</UserControl.Resources>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With