程序主要是要设置各种不同类型的参数。
 public abstract class ParamBase : INotifyPropertyChanged
    {
        public bool ThrowOnInvalidPropertyName { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
      
        protected void NotifyPropertyChanged(String propertyName)
        {
            this.VerifyPropertyName(propertyName);
            
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
定义一个参数泛型类,包含值和单位:public class ParamT<ValueType, UnitType> : ParamBase
    {
        public ParamT(ValueType valueType,UnitType unitType)
        {
            this._value = valueType;
            this._unit = unitType;
        }        private ValueType _value;  
        private UnitType _unit;        public ValueType Value
        {
            get { return this._value; }
            set
            {               
                this._value = value;
                base.NotifyPropertyChanged("Value");                
            }
        }            public UnitType Unit
        {
            get { return this._unit; }
            set
            {
                this._unit = value;
                base.NotifyPropertyChanged("Unit");
            }
        }
    }
定制参数设置的View,名为EditParam:
一个UserControl,里面有TextBlock显示参数名,TextBox设置参数,TextBlock显示单位名,一个定制按钮,弹出list,选择单位。参数是进行分类了的,比如主分类一
class SortA:ParamBase
{
 //里面有若干参数,
public ParamT<float, UnitTime> StartTime
        {
            get { return _startTime; }
            set
            {
                if (_startTime== value)
                    return;
                _startTime= value;               
                base.NotifyPropertyChanged("StartTime");
            }
        }
}
====
EditParam的TextBox的Text属性直接binding到SortA.StartTime.Value属性,
在这种情况下,我如何做数据校验?

解决方案 »

  1.   

    EditBox是要禁止无效字符输入的(扩展Editbox或者用依赖属性实现阻止),如果用binding校验的方式,因为没有无效字符的传递,所以就不会出现校验提醒!可不可以再Editbox本身阻断无效字符输入时,激发校验提醒,然后用校验显示机制显示错误提醒?
      

  2.   

    如果在TextBox_PreviewTextInput中根据设置的数值范围,阻断范围外的数值,那么用户在输入数值时就会很麻烦,所以不能这样做!
    如:范围是1000-3000,
    那么你输入1就自动变成1000了,如果输入1200就选中第二个0输入2,要不就大于3000了变成3000了!或者也可以再LostFocus事件中做。