Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a Localized string in the TargetNullValue attribute?

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'}"/>

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}} }"/>
like image 465
Albert Gao Avatar asked May 12 '12 15:05

Albert Gao


People also ask

What is the targetnullvalue property in the textblock?

The Binding's TargetNullValue property is displayed in the TextBlock if the [somveViewModel].SomeVar.SomeSubVar.Name property is null. It doesn't cane the value of the [somveViewModel].SomeVar.SomeSubVar.Name property.

How do I use the targetnullvalue behavior in a binding?

There are two recommended patterns for using TargetNullValue behavior in a Binding: The binding source provides a separate value that is accessed by a different path, which acts as the singleton value that can substitute for any null value coming from a specific data item in the source. For example:

How to set targetnullvalue in a WPF binding to an empty string?

How to set TargetNullValue in a WPF Binding to an empty string when the item is NULL. You can just set the TargetNullValue to an empty string using a Static. Be sure to add this to your class def at top. Happpy coding!

What is target null value in XAML?

Xaml. Data Gets or sets the value that is used in the target when the value of the source is null. The value that is used in the binding target when the value of the source is null. TargetNullValue might be used for bindings that bind a collection and the source data uses null for missing info only in some of the items.


2 Answers

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>

Finally, use it instead of TargetNullValue:

<TextBlock Text="{Binding DueDate, Converter={StaticResource NullValueConverter}, ConverterParameter=bt_help_Title1}" />
like image 57
Kevin Gosse Avatar answered Nov 09 '22 23:11

Kevin Gosse


Since you can't have a binding inside another binding you will need to use a multi-binding.

Something like:

<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
}
like image 34
Phil Avatar answered Nov 09 '22 23:11

Phil