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