我有一个转换器为空字符串提供了默认值。显然,你不能添加一个绑定到ConverterParameter,所以我添加一个属性到转换器,我将它绑定到。解决在Windows Phone上通过代码绑定XAML 8
但是,我回到默认属性的值是一个“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}}" />
任何想法?
完美 - 谢谢! – Gavin
感谢编辑,对不起,它被拒绝了,因为它是一个正确的编辑! –
不用担心 - 我想编辑代码以使生活更轻松,对于有同样问题的任何人;) – Gavin