using System.ComponentModel; using Telerik.WinControls.Data; //using System.Linq; namespace Pilz.UI.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 this.sourceControlPropertyName; } set { this.sourceControlPropertyName = value; } } /// /// Gets the Rule expression. /// /// The Rule expression. public override string Expression { get { if (this.TargetControl != null) { this.Value = this.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 (this.TargetControl != null && !string.IsNullOrEmpty(this.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 (this.TargetControl.Site != null) { return string.Format("{0}.{1}", this.TargetControl.Name, this.TargetControlPropertyName); } if (!string.IsNullOrEmpty(this.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; } }