在 WPF 绑定中将值设置为 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1895453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:19:46  来源:igfitidea点击:

Set value to null in WPF binding

wpfnullablebinding.net-3.5string

提问by Shimmy Weitzhandler

please take a look at the following line

请看下面的行

<TextBox Text="{Binding Price}"/>

This Price property from above is a Decimal?(Nullable decimal).

上面的这个 Price 属性是一个Decimal?(可空小数)。

I want that if user deletes the content of the textbox (i.e. enters empty string, it should automatcally update source with null (Nothing in VB).

我希望如果用户删除文本框的内容(即输入空字符串,它应该自动使用 null 更新源(VB 中没有)。

Any ideas on how I can do it 'Xamly'?

关于我如何做到“Xamly”的任何想法?

回答by Shimmy Weitzhandler

I am using .NET 3.5 SP1 so it's very simple:

我正在使用 .NET 3.5 SP1,所以它非常简单:

<TextBox Text="{Binding Price, TargetNullValue=''}"/>

Which stands for (thanks Gregor for your comment):

代表(感谢格雷戈尔的评论):

<TextBox Text="{Binding Price, TargetNullValue={x:Static sys:String.Empty}}"/>

sysis the imported xml namespace for Systemin mscorlib:

sys是为Systemin导入的 xml 命名空间mscorlib

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Hope that helped.

希望有所帮助。

回答by Thomas Levesque

This value converter should do the trick :

这个值转换器应该可以解决问题:

public class StringToNullableDecimalConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
        CultureInfo culture)
    {
        decimal? d = (decimal?)value;
        if (d.HasValue)
            return d.Value.ToString(culture);
        else
            return String.Empty;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        string s = (string)value;
        if (String.IsNullOrEmpty(s))
            return null;
        else
            return (decimal?)decimal.Parse(s, culture);
    }
}

Declare an instance of this converter in the ressources :

在 ressources 中声明这个转换器的一个实例:

<Window.Resources>
    <local:StringToNullableDecimalConverter x:Key="nullDecimalConv"/>
</Window.Resources>

And use it in your binding :

并在您的绑定中使用它:

<TextBox Text="{Binding Price, Converter={StaticResource nullDecimalConv}}"/>

Note that TargetNullValueis not appropriate here : it is used to define which value should be used when the sourceof the binding is null. Here Priceis not the source, it's a property of the source...

请注意,TargetNullValue此处不合适:它用于定义当source绑定的 为 null时应使用哪个值。这里Price不是源,它是源的属性......

回答by TimothyP

You can try using a ValueConverter (IValueConverter) http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

您可以尝试使用 ValueConverter (IValueConverter) http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

Of the back of my head here, something like:

在我的后脑勺这里,类似:

public class DoubleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        return (double)value;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
    var doubleValue = Convert.ToDouble(value);

    return (doubleValue == 0 ? null : doubleValue);
    }
}

(Might need some tweaking though)

(虽然可能需要一些调整)