为什么TargetNullValue更新为空的来源 [英] Why does TargetNullValue update nullable Source

查看:335
本文介绍了为什么TargetNullValue更新为空的来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TargetNullValue 应该更新绑定目标当结合来源计算结果为

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.

在除了它似乎也将来源(如果可能),当目标的价值是平等的给定 TargetNullValue 。换句话说,它有效地设置了 TargetNullValue 属性的值之间的等价。但是这不是文档都提到

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.

请参阅下面的例子:

<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();
    }
}

请注意: UpdateSourcetrigger 设置为仅使测试更加简单,并没有任何与有问题的效果。

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

如果你把一个断点 NullableInt 的制定者,你可以看到它被触发(与价值== NULL )当您更改文本框内容

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 ''.

这是一个无证行为 TargetNullValue 还是有在这里打球一些其他的副作用。

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

编辑:
我偶然发现了这个话题,是因为我看着这样一个问题:结果
设定值设置为null在WPF结合

推荐答案

这似乎是无证行为。如果你看一下 BindingExpressionBase.GetRawProposedValue() retreiving时要使用的值,它实际上检查是否该值等于TargetNullValue,如果是,使用代替

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;
}



(其中 EffectiveTargetNullValue 最终是 TargetNullValue )。

事实上,如果你设置你的 TargetNullValue 5 ,而不是一个空字符串,你会看到打字 5 将重置属性为null。

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.

这篇关于为什么TargetNullValue更新为空的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆