如何处理DependencyProperty溢出情况? [英] How do I handle DependencyProperty overflow situations?

查看:107
本文介绍了如何处理DependencyProperty溢出情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl 和一个名为Value的int DependencyProperty 。这绑定到 UserControl 上的文本输入。

I have a UserControl and an int DependencyProperty called Value. This is bound to a text input on the UserControl.

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(QuantityUpDown), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, CoerceValue));

public int Value
{
    get { return (int) GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
}

private static object CoerceValue(DependencyObject d, object basevalue)
{
    //Verifies value is not outside Minimum or Maximum
    QuantityUpDown upDown       = d as QuantityUpDown;
    if (upDown == null)
        return basevalue;

    if ((int)basevalue <= 0 && upDown.Instrument != null)
        return upDown.Minimum;

    //Stocks and ForEx can have values smaller than their lotsize (which is assigned to Minimum)
    if (upDown.Instrument != null && 
        upDown.Instrument.MasterInstrument.InstrumentType != Cbi.InstrumentType.Stock &&
        upDown.Instrument.MasterInstrument.InstrumentType != Cbi.InstrumentType.Forex)
        return Math.Max(Math.Min(upDown.Maximum, (int)basevalue), upDown.Minimum);

    if (upDown.Instrument == null)
        return Math.Max(Math.Min(upDown.Maximum, (int)basevalue), upDown.Minimum);

    if (upDown.Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.Stock ||
        upDown.Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.Forex)
        return Math.Min(upDown.Maximum, (int)basevalue);

    return basevalue;
}

如果用户输入的值大于 int。 MaxValue 在文本框中,当值进入 CoerceValue 时, baseValue 参数为1.如果我在 DependencyProperty 上提供验证值回调,也会发生同样的情况。

If a user enters a value greater than int.MaxValue in the text box, when the value comes into CoerceValue, the baseValue argument is 1. The same occurs if I provide a validation value callback on the DependencyProperty.

我想亲自处理这种情况,例如将传入值设置为 int.MaxValue 。有办法吗?

I'd like to handle this situation myself, such as setting the incoming value to int.MaxValue. Is there a way to do this?

推荐答案

int 属性可以切勿将其设置为 int 值以外的其他值。但是,TextBox的Text属性的类型为 string ,并且在运行时尝试设置您的 int 属性设置为字符串的值,该值表示无效的整数。

A int property can never be set to something else than an int value. The type of the Text property of a TextBox is however string and the error occurs when when the runtime is trying to set your int property to a string value that doesn't represent a valid integer.

您的依赖项属性不能做很多事情关于它,因为它永远不会被设置。正如@@ Ed Plunkett在他的评论中建议的那样,您可以在值转换发生之前使用ValidationRule进行操作,如果转换失败,则向用户显示错误消息:

Your dependency property cannot do much about this as it never gets set. As @@Ed Plunkett suggests in his comment you could use a ValidationRule to do something before the value conversion occurs and present an error message to the user if the conversion fails:

public class StringToIntValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        int i;
        if (int.TryParse(value.ToString(), out i))
            return new ValidationResult(true, null);

        return new ValidationResult(false, "Please enter a valid integer value.");
    }
}







<TextBox>
    <TextBox.Text>
        <Binding Path="Value" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <local:StringToIntValidationRule ValidationStep="RawProposedValue"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

有关更多信息,请参阅以下有关WPF中数据验证的博客文章: https://blog.magnusmontin.net/2013/08/26/data- wpf验证/

Please refer to the following blog post about data validation in WPF for more information: https://blog.magnusmontin.net/2013/08/26/data-validation-in-wpf/

这篇关于如何处理DependencyProperty溢出情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆