如何在TargetNullValue属性中绑定Localized字符串?

Alb*_*Gao 6 silverlight xaml windows-phone-7

我有一个Textblock,Text属性绑定到DateTime?类型数据,我想在DateTime时显示一些东西?数据为空.
下面的代码效果很好.

  < TextBlock Text="{Binding DueDate, TargetNullValue='wow,It's null'}"/>
Run Code Online (Sandbox Code Playgroud)

但是,如果我想将Localizedstring绑定到TargetNullValue呢?
下面的代码不起作用:(
怎么样?

  < TextBlock Text="{Binding DueDate, TargetNullValue={Binding LocalStrings.bt_help_Title1, Source={StaticResource LocalizedResources}} }"/>
Run Code Online (Sandbox Code Playgroud)

Kev*_*sse 4

我没有看到任何方法可以使用 TargetNullValue 来做到这一点。作为解决方法,您可以尝试使用转换器:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将其添加到页面的资源中:

<phone:PhoneApplicationPage.Resources>
    <local:NullValueConverter x:Key="NullValueConverter" />
</phone:PhoneApplicationPage.Resources>
Run Code Online (Sandbox Code Playgroud)

最后,使用它代替 TargetNullValue:

<TextBlock Text="{Binding DueDate, Converter={StaticResource NullValueConverter}, ConverterParameter=bt_help_Title1}" />
Run Code Online (Sandbox Code Playgroud)