Why does TargetNullValue update nullable Source
Asked Answered
B

1

8

TargetNullValue is supposed to update the binding Target when the binding Source evaluates to null:

Gets or sets the value that is used in the target when the value of the source is null.

In addition to that it also appears to set the Source to null (if possible), when the value of Target is equals to given TargetNullValue. In other words, it effectively sets up an equivalency between null and the TargetNullValue property's value. However this is not mentioned in the documentation at all.

See this example:

<Window x:Class="WPF_Sandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_Sandbox"
        Title="MainWindow"
        x:Name="ThisControl">
    <StackPanel x:Name="MainStackPanel">
        <TextBox x:Name="MyTextBox" Text="{Binding NullableInt, ElementName=ThisControl, TargetNullValue='', UpdateSourceTrigger=PropertyChanged}" />
    </StackPanel>
</Window>


public partial class MainWindow : Window
{
    private int? nullableInt;

    public int? NullableInt
    {
        get { return nullableInt; }
        set { nullableInt = value; }
    }

    public MainWindow()
    {
        InitializeComponent();
    }
}

Note: UpdateSourcetrigger is only set to make testing easier and has nothing to do with the effect in question.

If you put a breakpoint in NullableInt's setter, you can see that it gets triggered (with value == null) when you change the TextBox content to ''.

Is this a undocumented behavior by TargetNullValue or is there some other side effect in play here?

Edit: I stumbled upon this topic, because I was looking at this question:
Set value to null in WPF binding

Bogbean answered 4/4, 2016 at 12:11 Comment(0)
I
9

It seems to be undocumented behavior. If you look at BindingExpressionBase.GetRawProposedValue() when retreiving the value to use, it actually checks to see if the value is equal to the TargetNullValue and if so, uses null instead:

internal virtual object GetRawProposedValue()
{
    object value = Value;

    // TargetNullValue is the UI representation of a "null" value.  Use null internally.
    if (Object.Equals(value, EffectiveTargetNullValue))
    {
        value = null;
    }

    return value;
}

(where EffectiveTargetNullValue is ultimately the TargetNullValue).

Indeed, if you set your TargetNullValue to be 5 instead of an empty string, you'll see that typing 5 will reset the property to null.

Ignominious answered 6/4, 2016 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.