using System.ComponentModel; //using System.Linq; namespace Pilz.UI.WinForms.Telerik.Controls.RadValidationProvider; /// /// RadValidationRuleWithTargetControl provides a validation logic which compares RadControl's Property with TargetControl's property. /// public class RadValidationRuleWithTargetControlEx : RadValidationRuleEx { private string sourceControlPropertyName = "Text"; public RadValidationRuleWithTargetControlEx() { } /// /// Gets or sets the Target Control. This control's property value will be used in the Rule evaluation. /// [DefaultValue(null)] public Control TargetControl { get; set; } /// /// The name of the property that will be used in the Rule evaluation. /// [DefaultValue("Text")] public string TargetControlPropertyName { get { return sourceControlPropertyName; } set { sourceControlPropertyName = value; } } /// /// Gets the Rule expression. /// /// The Rule expression. public override string Expression { get { if (TargetControl != null) Value = CalculateValue(); return base.Expression; } } /// /// Gets or sets the Value of this rule. Controls in the rule will be evaluated against this value. /// [EditorBrowsable(EditorBrowsableState.Never)] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public override object Value { get { if (TargetControl != null && !string.IsNullOrEmpty(TargetControlPropertyName)) { try { base.Value = RadValidationProviderEx.GetSubPropertyValue(TargetControl, TargetControlPropertyName);//TargetControl.GetType().GetProperty(TargetControlPropertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(TargetControl, null); } catch { } } return base.Value; } set { base.Value = value; } } protected virtual object CalculateValue() { if (TargetControl.Site != null) return string.Format("{0}.{1}", TargetControl.Name, TargetControlPropertyName); if (!string.IsNullOrEmpty(TargetControlPropertyName)) { try { return RadValidationProviderEx.GetSubPropertyValue(TargetControl, TargetControlPropertyName);//this.TargetControl.GetType().GetProperty(this.TargetControlPropertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(this.TargetControl, null); } catch { } } return null; } }