Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
248 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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>

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...