6

Text属性がDateTimeにバインドしているTextblockがありますか?データを入力し、DateTimeのときに何かを表示したいですか?データがnullです。
以下のコードはうまく機能します。

  < TextBlock Text="{Binding DueDate, TargetNullValue='wow,It's null'}"/>

しかし、LocalizedstringをTargetNullValueにバインドしたい場合はどうでしょうか?
以下のコードは機能しません:(
方法は?

  < TextBlock Text="{Binding DueDate, TargetNullValue={Binding LocalStrings.bt_help_Title1, Source={StaticResource LocalizedResources}} }"/>
4

2 に答える 2

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();
    }
}

次に、それをページのリソースに追加します。

<phone:PhoneApplicationPage.Resources>
    <local:NullValueConverter x:Key="NullValueConverter" />
</phone:PhoneApplicationPage.Resources>

最後に、TargetNullValueの代わりに使用します。

<TextBlock Text="{Binding DueDate, Converter={StaticResource NullValueConverter}, ConverterParameter=bt_help_Title1}" />
于 2012-05-12T17:08:10.527 に答える
1

別のバインディング内にバインディングを含めることはできないため、マルチバインディングを使用する必要があります。

何かのようなもの:

<Window.Resources>
    <local:NullConverter x:Key="NullConverter" />
</Window.Resources>

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource NullConverter}">
            <Binding Path="DueDate"/>
            <!-- using a windows resx file for this demo -->
            <Binding Source="{x:Static local:LocalisedResources.ItsNull}" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

public class NullConverter : IMultiValueConverter
{
    #region Implementation of IMultiValueConverter

    public object Convert(object[] values, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        if (values == null || values.Length != 2)
        {
            return string.Empty;
        }

        return (values[0] ?? values[1]).ToString();
    }

    public object[] ConvertBack(object value, Type[] targetTypes, 
                                object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
于 2012-05-12T17:08:30.800 に答える