public static void SetBindingObject(FrameworkElement obj, BindingMode mode, object source, DependencyProperty dp, string propertyName) { Type propertyType = source.GetType().GetProperty(propertyName).PropertyType; PropertyInfo info = source.GetType().GetProperty("RangeList"); FANumberRangeRule rangeRule = null; if (info != null) { object value = info.GetValue(source, null); if (value != null) { FALibrary.Utility.SerializableDictionary<string, FARange> dic = (FALibrary.Utility.SerializableDictionary<string, FARange>)value; if (dic.ContainsKey(propertyName)) { rangeRule = new FANumberRangeRule(propertyType); rangeRule.Range = dic[propertyName]; obj.Tag = rangeRule; obj.Style = (Style)App.Current.Resources["TextBoxErrorStyle"]; } } } Binding bd = new Binding(propertyName); bd.Source = source; bd.Mode = mode; bd.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; if (rangeRule != null) { bd.NotifyOnValidationError = true; bd.ValidationRules.Add(rangeRule); } obj.SetBinding(dp, bd); }
private static Utility.FANumberRangeRule GetNumberRangeRule(TextBox textBox, out bool isString) { try { isString = false; var bindingEx = BindingOperations.GetBindingExpression(textBox, TextBox.TextProperty); object source; string propertyName; GetObjectAndProperty(bindingEx.DataItem, bindingEx.ParentBinding.Path.Path, out source, out propertyName); if (source == null || string.IsNullOrEmpty(propertyName)) return null; var propertyType = source.GetType().GetProperty(propertyName).PropertyType; if (propertyType.IsPrimitive == false) { isString = true; return null; } FANumberRangeRule rangeRule = new FANumberRangeRule(propertyType); var rangeListPropertyInfo = source.GetType().GetProperty("RangeList"); var propertyValue = rangeListPropertyInfo.GetValue(source, null); if (propertyValue == null) return rangeRule; var rangeList = (FALibrary.Utility.SerializableDictionary<string, FALibrary.FARange>)propertyValue; if (rangeList == null) return rangeRule; if (rangeList.ContainsKey(propertyName)) { rangeRule = new FANumberRangeRule(propertyType); rangeRule.Range = rangeList[propertyName]; } return rangeRule; } catch { isString = true; return null; } }