如何在 TargetNullValue 属性中绑定本地化字符串? [英] How to bind a Localized string in the TargetNullValue attribute?
问题描述
我有一个 Textblock,其中 Text 属性绑定到 DateTime?输入数据,我想在DateTime时显示一些东西?数据为空.
下面的代码效果很好.
I have a Textblock which Text attribute is binding to a DateTime? type data, and I want to show something when the DateTime? data is null.
The code below works great.
< TextBlock Text="{Binding DueDate, TargetNullValue='wow,It's null'}"/>
但是如果我想将 Localizedstring 绑定到 TargetNullValue 呢?
下面的代码不起作用:(
如何?
But what about if I want to bind a Localizedstring to the TargetNullValue?
The Code below not work :(
How to?
< TextBlock Text="{Binding DueDate, TargetNullValue={Binding LocalStrings.bt_help_Title1, Source={StaticResource LocalizedResources}} }"/>
推荐答案
我认为 TargetNullValue 没有任何方法可以做到这一点.作为解决方法,您可以尝试使用转换器:
I don't see any way to do that with TargetNullValue. As a workaround, you can try using a converter:
public class NullValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
return value;
}
var resourceName = (string)parameter;
return AppResources.ResourceManager.GetString(resourceName);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后将其添加到您页面的资源中:
Then add it to the resources of your page:
<phone:PhoneApplicationPage.Resources>
<local:NullValueConverter x:Key="NullValueConverter" />
</phone:PhoneApplicationPage.Resources>
最后,用它代替 TargetNullValue:
Finally, use it instead of TargetNullValue:
<TextBlock Text="{Binding DueDate, Converter={StaticResource NullValueConverter}, ConverterParameter=bt_help_Title1}" />
这篇关于如何在 TargetNullValue 属性中绑定本地化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!