[英]Resolve XAML Binding through Code on Windows Phone 8
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. 显然您不能向ConverterParameter添加绑定,所以我向转换器添加了一个属性,而我绑定了该属性。
However, the value I'm getting back for the default property is a string of "System.Windows.Data.Binding" instead of my value. 但是,我为默认属性返回的值是字符串“ System.Windows.Data.Binding”而不是我的值。
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 ): 这是我的转换器类(基于答案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: 而我的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 . 您应该能够通过继承来完成此DependencyObject的和改变你的Default
属性是一个的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));
...
...
For now I've resolved the issue by inheriting from my converter and setting the localized string in the constructor. 现在,我已经通过继承转换器并在构造函数中设置本地化字符串来解决了该问题。 However, I feel there's must be a more elegant solution to my problem that allows the base converter to be used directly. 但是,我觉得必须有一个更优雅的解决方案来允许我直接使用基本转换器。
public class WaypointNameConverter : DefaultForNullOrWhiteSpaceStringConverter
{
public WaypointNameConverter()
{
base.Default = Resources.AppResources.Waypoint_NoName;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.