I have a converter that provides a default value for empty strings. Apparently you can't add a binding to the ConverterParameter so I add a property to the converter, which I bind to instead.
However, the value I'm getting back for the default property is a string of "System.Windows.Data.Binding" instead of my value.
How do I resolve this binding in code so I can return the real localized string I want?
Here's my converter class (based on answer https://stackoverflow.com/a/15567799/250254):
public class DefaultForNullOrWhiteSpaceStringConverter : IValueConverter
{
public object Default { set; get; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!string.IsNullOrWhiteSpace((string)value))
{
return value;
}
else
{
if (parameter != null)
{
return parameter;
}
else
{
return this.Default;
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
And my XAML:
<phone:PhoneApplicationPage.Resources>
<tc:DefaultForNullOrWhiteSpaceStringConverter x:Key="WaypointNameConverter"
Default="{Binding Path=LocalizedResources.Waypoint_NoName, Mode=OneTime, Source={StaticResource LocalizedStrings}}" />
</phone:PhoneApplicationPage.Resources>
<TextBlock Text="{Binding Name, Converter={StaticResource WaypointNameConverter}}" />
Any ideas?
You should be able to accomplish this by inheriting from DependencyObject and changing your Default
property to be a DependencyProperty.
public class DefaultForNullOrWhiteSpaceStringConverter : DependencyObject, IValueConverter
{
public string DefaultValue
{
get { return (string)GetValue(DefaultValueProperty); }
set { SetValue(DefaultValueProperty, value); }
}
// Using a DependencyProperty as the backing store for DefaultValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DefaultValueProperty =
DependencyProperty.Register("DefaultValue", typeof(string),
typeof(DefaultForNullOrWhiteSpaceStringConverter), new PropertyMetadata(null));
...
...