是否可以使用设置的 TargetNullValue 更新数据绑定源值,以防它是 'null'?

Is it possible to update a databinding source value with the set TargetNullValue in case it is 'null'?

只是出于兴趣....

如果我有一个带有未初始化字符串的 ViewModel,它绑定到一个文本框,我可以使用 TargetNullValue 来显示默认值。 但是,我想知道我是否可以使用相同的值来更新字符串,以防它是 null?

基本上代替了

    set
    {
        if(value != null) text = value;
        else value = "defaultstring";
        OnPropertyChanged();
    }  

只需使用 TargetNullValue 从数据绑定中做同样的事情。

您可以操作 getter 以及数据绑定将使用 get():

    private string text;

    public string Text
    {
        get
        {
            if (text== null)
                return "default value";
            else
                return this.text;
        }
        set { this.text= value; }

    }

但是,如果您想在纯 XAML 中执行此操作,您可以为此使用 DataTrigger:

<TextBlock Text="{Binding MyText}">
   <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock }">
            <Style.Triggers>
                <DataTrigger Binding="{Binding MyText}" Value="{x:Null}">
                    <Setter Property="Text" Value="DefaultValue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>