Establecer valor en null en el enlace WPF


Por favor, eche un vistazo a la siguiente línea

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

Esta propiedad de Precio desde arriba es un Decimal? (decimal nulo).

Quiero que si el usuario borra el contenido del cuadro de texto (es decir, ingresa una cadena vacía, debe actualizar automáticamente el origen con null (Nada en VB).

¿Alguna idea sobre cómo puedo hacerlo 'Xamly'?

Author: skaffman, 2009-12-13

3 answers

Estoy usando. NET 3.5 SP1 por lo que es muy simple:

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

Que significa (gracias Gregor por su comentario):

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

sys es el espacio de nombres xml importado para System en mscorlib:

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

Espero que eso haya ayudado.

 208
Author: Shimmy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-01 17:09:21

Este convertidor de valores debería hacer el truco:

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 una instancia de este convertidor en los recursos:

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

Y úsalo en tu encuadernación:

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

Tenga en cuenta que TargetNullValue no es apropiado aquí : se usa para definir qué valor debe usarse cuando el source del enlace es nulo. Aquí Price no es la fuente, es una propiedad de la fuente...

 11
Author: Thomas Levesque,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-24 08:01:53

Puede intentar usar un ValueConverter (IValueConverter) http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

De la parte posterior de mi cabeza aquí, algo como:

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

(Aunque podría necesitar algunos ajustes)

 5
Author: TimothyP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-12-13 03:31:06