Why does TargetNullValue update nullable Source

二次信任 提交于 2020-01-29 06:59:25

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/36402291/why-does-targetnullvalue-update-nullable-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!