Reputation: 4905
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.
Upvotes: 1
Views: 850
Reputation: 132558
What's the order of your objects in your XAML?
The Converter
has to be defined prior to actually being used, so be sure your <Converter>
is above your <DataTemplate>
in your Resources
Another alternative is to switch to using a DynamicResource
instead of a StaticResource
, since a DynamicResource
is evaluated when it is needed, not when the XAML is loaded
Upvotes: 4
Reputation: 9244
That error is usually thrown when it can't find the static resource you are looking for. You'll need to define that in your static resources.
<Window
.... snip ...
xmlns:local="clr-namespace:YourLocalNamespace"
<Window.Resources>
<local:BodyValueConverter x:Key="BodyValueConverter"/>
</Window.Resources>
.... snip ....
<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>
</Window>
Note: This is when you are defining it in Window. You could define it elsewhere.
If this isn't the issue.... to find a more detailed explanation of what the parse error is... check the inner exception text.
Upvotes: 1