Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TargetNullValue update nullable Source

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

like image 857
Tim Pohlmann Avatar asked Apr 04 '16 12:04

Tim Pohlmann


People also ask

What is fallback value in WPF?

The FallbackValue is used if the binding source path does not resolve, if the converter fails, or if the value is not valid for the property's type.

What is TargetNullValue?

Solution - TargetNullValue This is where we use TargetNullValue: TargetNullValue is set to thecontrol's bound property when the value of the bound property also known as source property is null for some reason. In layman's terms, when the target is null, this property will be assigned.


1 Answers

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.

like image 103
zastrowm Avatar answered Sep 19 '22 21:09

zastrowm