检测自定义 wpf 文本框上的绑定错误

Detect binding error on a custom wpf textbox

我有一个自定义文本框,它有一个名为 valueProperty 的 dependencyProperty,类型为 double nullable。我的问题是 属性 绑定到模型上的双倍可空值和不可空值,当我尝试放置一个空值并且绑定值不可空时,这显然失败了,显示红色矩形。我想检测绑定失败并在发生这种情况时分配一个 0。所以我的问题是:有什么方法可以检测到绑定失败吗?

我知道我可以使用 2 个不同的 customsTextbox 为 nullables 和 no nullables 以及其他方式修复它,只是想知道是否有办法检查绑定是否成功。提前致谢。

编辑>>>>>

型号:

private double _Temperature;
public double Temperature
{
    get { return _Temperature; }
    set { SetProperty(ref this._Temperature, value); }
}

private double? _Density;
public double? Density
{
    get { return _Density; }
    set { SetProperty(ref this._Density, value); }
}

查看(简体):

<local:customTextBox Value="{Binding Temperature}"/>
<local:customTextBox Value="{Binding Density}"/>

customTextBox 依赖属性:

public static readonly DependencyProperty valueProperty =            
DependencyProperty.RegisterAttached(
        "Value",
        typeof(double?),
        typeof(customTextBox),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValuePropertyChanged)
        );

    private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        customTextBox ctb = d as customTextBox;
        ntb.Value = (double?)e.NewValue;
        //Here I can check the binding fail.
    }

用解决方案编辑>>>>

我的问题有不同的解决方案,我将一一列举:

@blindmeis解决方案。这是最简单的一个,但效果较差:

<local:customTextBox Value="{Binding Temperature, TargeNullValue=0}"/>

@Gary H解决方案。这是我选择的解决方案,因为它完全回答了我的问题,而且在我当前的应用程序中更容易实现:

private static void OnValuePropertyChanged(DependencyObject d,     
DependencyPropertyChangedEventArgs e)
{
    customTextBox ctb = d as customTextBox;
    ntb.Value = (double?)e.NewValue;
    if (Validation.GetHasError(d)) 
        {
            //The binding have failed
        }
}

@tomab解决方案。我认为使用转换器是一个很好的解决方案(也许更好),但由于其他依赖属性,我仍然需要保留 customTextBox class,我将需要重构这么多代码。我会在以后的实施中牢记这一点。

谢谢大家的帮助。

这是一种使用转换器的方法,它的优点是您可以根据您需要的任何逻辑控制您想要的输出值(将显示)的方式。

public class DoubleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        // if you want to handle `double` too you can uncomment following lines but this is an ugly hack
        // if (value != null && value.GetType() == typeof(double))
        // {
        //    return value;
        // }
        var nullableDouble = (double?)value;
        if (nullableDouble.HasValue)
        {
            return nullableDouble.Value;
        }
        return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

xaml 可能如下所示:

<UserControl.Resources>
    <converters:DoubleConverter x:Key="DoubleConverter"/>
</UserControl.Resources>

<TextBox Text="{Binding SomeValue, Converter={StaticResource DoubleConverter}}" />

SomeValue 必须是 double?.

类型

是的,这是一个验证错误。 您可以查询附件 属性:

var errors=Validation.GetErrors(myTextbox);

有关处理和自定义验证的信息,请参阅: Data validation in WPF