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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…