I have the following converter defined (C#):
class BodyValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string s = value.ToString();
int prefixLength;
if (!int.TryParse(parameter.ToString(), out prefixLength))
return s;
return s.Substring(0, prefixLength);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
This will start at the start of the string being passed and will return the amount of characters I specify as a parameter.
In my XAML I have instanced the converter:
<local:BodyValueConverter x:Key="BodyValueConverter"/>
In attempting to use this converter in a textblock I get an error:
<DataTemplate x:Key="AppointmentTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Subject}"></TextBlock>
<TextBlock Text="{Binding Path=Subject, Converter={StaticResource BodyValueConverter}, ConverterParameter=1}"></TextBlock>
</StackPanel>
</DataTemplate>
The error is:
XAMLParseException: Provide value on ‘System.Windows.Markup.StaticResourceHolder’ threw an exception.
The first textblock works fine to display subject. The 2nd line is what gives me the exception.
What’s the order of your objects in your XAML?
The
Converterhas to be defined prior to actually being used, so be sure your<Converter>is above your<DataTemplate>in yourResourcesAnother alternative is to switch to using a
DynamicResourceinstead of aStaticResource, since aDynamicResourceis evaluated when it is needed, not when the XAML is loaded