【发布时间】:2013-06-18 02:59:12
【问题描述】:
我有一个为空字符串提供默认值的转换器。显然,您无法将绑定添加到 ConverterParameter,因此我向转换器添加了一个属性,而是绑定到该属性。
但是,我为默认属性返回的值是“System.Windows.Data.Binding”字符串,而不是我的值。
如何在代码中解决这个绑定,以便返回我想要的真正的本地化字符串?
这是我的转换器类(基于答案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();
}
}
还有我的 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}}" />
有什么想法吗?
【问题讨论】:
标签: c# xaml windows-phone-8 ivalueconverter