繁体   English   中英

数据绑定异常

[英]DataBinding Exceptions

在 wpf 应用程序中,我有数据绑定:文本框到 int 和带有 INPC 的双属性。

应用程序逻辑必须在 UpdateSourceTrigger 处使用“PropertyChanged”。 还实现了带有数据注释属性的 INotifyDataErrorInfo 接口。

当我清除文本框时,我在调试 output window 中看到数据绑定异常。

System.Windows.Data Error: 7 : ConvertBack cannot convert value '' (type 'String'). BindingExpression:Path=Power; DataItem='Auto' (HashCode=64554036); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') FormatException:'System.FormatException: Input string has invalid format.
   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.String.System.IConvertible.ToDouble(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at MS.Internal.Data.SystemConvertConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'

我觉得不好。

我可以包装 model class (装饰器模式)并在这个 class 中具有良好的验证逻辑。 只需添加字符串属性和逻辑即可避免数据绑定异常。

避免这些例外情况有多重要?

对于 model class 是否应该包装在这种情况下? 这种情况下的最佳做法是什么?

我的示例代码有这个问题。

<TextBox Text="{Binding Power, UpdateSourceTrigger=PropertyChanged}"/>
public class Auto : INotifyPropertyChanged
    {
        string _model;
        private double _power;
        private int _volume;

        public event PropertyChangedEventHandler PropertyChanged;

        private void Set<T>(ref T field, T value, [CallerMemberName]string propertyName = "")
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return;
            field = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public string Model
        {
            get => _model;
            set => Set(ref _model, value);
        }
        public double Power
        {
            get => _power;
            set => Set(ref _power, value);
        }
        public int Volume
        {
            get => _volume;
            set => Set(ref _volume, value);
        }
    }

避免这些例外情况有多重要?

并不重要,因为框架已经为您处理了它们。

对于 model class 是否应该包装在这种情况下?

不,不要使用string属性来保存intdouble值。

这种情况下的最佳做法是什么?

忽略绑定错误或实现您自己的转换器:

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

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!double.TryParse(value.ToString(), out double d))
            return Binding.DoNothing;
        return d;
    }
}

用法:

<TextBox>
    <TextBox.Text>
        <Binding Path="Power" UpdateSourceTrigger="PropertyChanged">
            <Binding.Converter>
                <local:DoubleConverter />
            </Binding.Converter>
        </Binding>
    </TextBox.Text>
</TextBox>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM