private List <ControlObject> GetControls(BSkyCanvas canvas) { List <ControlObject> lst = new List <ControlObject>(); foreach (Object obj in canvas.Children) { if (obj is IBSkyInputControl) { IBSkyInputControl ib = obj as IBSkyInputControl; lst.Add(new ControlObject() { Name = ib.Name, Type = ib.GetType().Name }); } if (obj is BSkyButton) { FrameworkElement fe = obj as FrameworkElement; BSkyCanvas cs = fe.Resources["dlg"] as BSkyCanvas; if (cs != null) { List <ControlObject> lstTemp = GetControls(cs); foreach (ControlObject co in lstTemp) { co.Parent = ((BSkyButton)obj).Text; } lst.AddRange(lstTemp); } } } return(lst); }
public void ApplyBehaviour(object sender, BehaviourCollection behaviours) { //A control is associated with a behavior collection //A behavior collection is a collection of behaviors //A behavior can contains one condition e.g. itemscount = 0 and one or more setters e.g. set the property canexecute =true on control destination. The setter also stores the //value of the property its bound to (the control and the propert name) //Behaviors are invoked on the control when a particular activity takes place for e.g. when a selection changes on a listbox control, when text is entered //on a text control. //Typical use case for behaviors is as follows. Every control has a CanExecute property. This can be enabled or disabled by default. //The OK button will only be enabled when the can execute property of all the controls on the canvas are set to true. // //For example, when the //one sample t.test dialog is rendered for the first time, the destination listbox will always be empty. We would then set the default value of canexecute to false. //We would define a condition for when items count is > than 0 and set canexecute for the destination property to true only when items count is > than 0. //We would also add another condition to set Canexecute to false when items count is 0 to account for the case when users move variables from the destination //back to the source. //Also note that you can have canexecute to true on the destination control of one sample t.test but have the canexecute property on the textbox set to false //as no valid entry has be entered tocompare the dialog against. In this situation the OK button on the dialog is disabled foreach (Behaviour behaviour in behaviours) { //Get the type of object where the selection has changed //Added by Aaron 04/12/2014 //The code below to make sure that the property type matches the value type. This insures that the //condition can be applied without any exceptionsto which the property IBSkyInputControl ctrl = sender as IBSkyInputControl; Type t = sender.GetType(); if (behaviour.Condition.PropertyName == null) { System.Windows.Forms.MessageBox.Show(BSky.GlobalResources.Properties.Resources.CanvasPropNotSet1 + " \"" + ctrl.Name + "\"" + BSky.GlobalResources.Properties.Resources.CanvasPropNotSet2 + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.CanvasPropNotSet3); return; } //Get information of the property on which the condition is specified. The property belongs to the object PropertyInfo pInfo = t.GetProperty(behaviour.Condition.PropertyName); if (pInfo == null) { continue; } //Get the value of the property for which the condition is triggered object value = pInfo.GetValue(sender, null); bool pass = false; //why can't I call pInfo.GetType() below? //Store the type of the property for which the condition is defined Type propType = pInfo.PropertyType; if (propType == typeof(int) || propType == typeof(double) || propType == typeof(long)) { try { double lvalue = Convert.ToDouble(value); double rvalue = Convert.ToDouble(behaviour.Condition.Value); } catch (FormatException e) { System.Windows.Forms.MessageBox.Show(BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect1 + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect2 + " \n" + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect3 + " \"" + behaviour.Condition.PropertyName + "\" " + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect4 + " \"" + behaviour.Condition.Value + "\"\n" + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect5); return; } } if (propType == typeof(Boolean)) { try { Boolean lvalue = Convert.ToBoolean(value); Boolean rvalue = Convert.ToBoolean(behaviour.Condition.Value); } catch (FormatException e) { System.Windows.Forms.MessageBox.Show(BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect1 + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect2 + "\n" + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect3 + " \"" + behaviour.Condition.PropertyName + "\" " + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect4 + " \"" + behaviour.Condition.Value + "\"\n" + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect5); return; } } //Code below sets pass =true if the condition specified is triggered switch (behaviour.Condition.Operator) { //Added by Aaron: 08/25/2013 //the equals operator appliesto strings (textboxes) and numeric types (itemscount on a listbox) case ConditionalOperator.Equals: if (propType == typeof(int) || propType == typeof(double) || propType == typeof(long) || propType == typeof(string) || propType == typeof(Boolean)) { if (propType == typeof(int) || propType == typeof(double) || propType == typeof(long)) { double lvalue = Convert.ToDouble(value); double rvalue = Convert.ToDouble(behaviour.Condition.Value); if (lvalue == rvalue) { pass = true; } } if (propType == typeof(string)) { string lvalue = value as string; string rvalue = behaviour.Condition.Value; if (lvalue == rvalue) { pass = true; } } if (propType == typeof(Boolean)) { Boolean lvalue = Convert.ToBoolean(value); Boolean rvalue = Convert.ToBoolean(behaviour.Condition.Value); if (lvalue == rvalue) { pass = true; } } } break; case ConditionalOperator.GreaterThan: if (propType == typeof(int) || propType == typeof(double) || propType == typeof(long)) { double lvalue = Convert.ToDouble(value); double rvalue = Convert.ToDouble(behaviour.Condition.Value); if (lvalue > rvalue) { pass = true; } } break; case ConditionalOperator.GreaterThanEqualsTo: if (propType == typeof(int) || propType == typeof(double) || propType == typeof(long)) { double lvalue = Convert.ToDouble(value); double rvalue = Convert.ToDouble(behaviour.Condition.Value); if (lvalue >= rvalue) { pass = true; } } break; case ConditionalOperator.LessThan: if (propType == typeof(int) || propType == typeof(double) || propType == typeof(long)) { double lvalue = Convert.ToDouble(value); double rvalue = Convert.ToDouble(behaviour.Condition.Value); if (lvalue < rvalue) { pass = true; } } break; case ConditionalOperator.LessThanEqualsTo: if (propType == typeof(int) || propType == typeof(double) || propType == typeof(long)) { double lvalue = Convert.ToDouble(value); double rvalue = Convert.ToDouble(behaviour.Condition.Value); if (lvalue >= rvalue) { pass = true; } } break; case ConditionalOperator.Contains: if (propType == typeof(string)) { string lvalue = value.ToString(); string rvalue = behaviour.Condition.Value.ToString(); if (lvalue.Contains(rvalue)) { pass = true; } } break; case ConditionalOperator.Like: if (propType == typeof(string)) { string lvalue = value.ToString(); string rvalue = behaviour.Condition.Value.ToString(); if (lvalue.StartsWith(rvalue)) { pass = true; } } break; //Added by Aaron on 08/25/2013 //We want the textbox on One Sample T.test disabled unless a valid numeric value is entered to compare against case ConditionalOperator.IsNumeric: if (propType == typeof(string)) { decimal number = 0; string valueasstring = value as string; bool canConvert = Decimal.TryParse(valueasstring, out number); if (canConvert == true) { pass = true; } } break; case ConditionalOperator.NotNumeric: if (propType == typeof(string)) { decimal number = 0; string valueasstring = value as string; bool canConvert = Decimal.TryParse(valueasstring, out number); if (canConvert == false) { pass = true; } } break; //Added by Aaron 12/12/2013 //This is to support a condition of valid string to ensure a textbox has a valid string entered case ConditionalOperator.ValidString: if (propType == typeof(string)) { string valueasstring = value as string; if (valueasstring != null && valueasstring != "") { pass = true; } } break; case ConditionalOperator.isNullOrEmpty: if (propType == typeof(string)) { string valueasstring = value as string; if (valueasstring == null || valueasstring == "") { pass = true; } } break; } if (pass) { foreach (PropertySetter setter in behaviour.Setters) { //Added by Aaron 05/16/2015 //Its very easily to create a null property and control name and value set //you click add a setter and we create a control name, property name and value to null //The if look ensures that we don't crash if (setter.ControlName != null || setter.PropertyName != null) { object fe = null; if (string.IsNullOrEmpty(setter.ControlName)) { fe = this; } else { fe = this.getCtrl(setter.ControlName); // fe = this.FindName(setter.ControlName); } //Aaron 04/06/2014 // Error message needs to inserted here if (fe == null) { System.Windows.Forms.MessageBox.Show(BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect1 + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect2 + "\n" + BSky.GlobalResources.Properties.Resources.TheCtrlName + " \"" + setter.ControlName + "\" " + BSky.GlobalResources.Properties.Resources.CantFind + BSky.GlobalResources.Properties.Resources.CheckBehaviour + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.InDialogEditor); return; } Type feType = fe.GetType(); //Aaron 11/03/2013 //fepinfo has has the destination property that needs to be set PropertyInfo fepInfo = feType.GetProperty(setter.PropertyName); if (fepInfo == null) { System.Windows.Forms.MessageBox.Show(BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect1 + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.CtrlBehaviourIncorrect2 + " \n" + BSky.GlobalResources.Properties.Resources.ThePropName + " \"" + setter.PropertyName + "\" " + BSky.GlobalResources.Properties.Resources.NotValidCtrlProp + " \"" + setter.ControlName + "\" " + BSky.GlobalResources.Properties.Resources.CheckBehaviour + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.InDialogEditor); return; } // Added by Aaron11/03/2013 //Commented the section below //if (!setter.IsBinding) //{ // //Aaron 11/03/2013 // //sets the value of the destination property // fepInfo.SetValue(fe, Convert.ChangeType(setter.Value, fepInfo.PropertyType), null); //} //else //{ // //Aaron 11/03/2013 // //gets the source control name // object source = this.FindName(setter.SourceControlName); // if (source == null) // return; // Type sourceType = source.GetType(); // //gets the property from the source control // PropertyInfo sourcePropInfo = sourceType.GetProperty(setter.SourcePropertyName); // //gets the value of the property from the source control // object val = sourcePropInfo.GetValue(source, null); // //Sets the destination value to the value of the property from the source control // fepInfo.SetValue(fe, Convert.ChangeType(val, fepInfo.PropertyType), null); //} try { fepInfo.SetValue(fe, Convert.ChangeType(setter.Value, fepInfo.PropertyType), null); if (fepInfo == null) { System.Windows.Forms.MessageBox.Show(BSky.GlobalResources.Properties.Resources.TheValue + " \"" + setter.Value + "\" " + BSky.GlobalResources.Properties.Resources.CantAssignToProp + " \"" + setter.PropertyName + "\" " + BSky.GlobalResources.Properties.Resources.CheckBehaviour + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.InDialogEditor); } } catch (FormatException e) { System.Windows.Forms.MessageBox.Show(BSky.GlobalResources.Properties.Resources.TheExceptionGenerated + " \"" + e.Message + "\"" + "\n" + BSky.GlobalResources.Properties.Resources.ThePropName + " \"" + setter.PropertyName + "\" " + BSky.GlobalResources.Properties.Resources.InCtrl + " \"" + setter.ControlName + "\" " + BSky.GlobalResources.Properties.Resources.CantSetValue + " \"" + setter.Value + "\" " + BSky.GlobalResources.Properties.Resources.PropCantAcceptVal + "\n" + BSky.GlobalResources.Properties.Resources.CheckBehaviour + " \"" + ctrl.Name + "\" " + BSky.GlobalResources.Properties.Resources.InDialogEditor); } } } //Added by Aaron 05/16/2015, this is the end of the for loop } } CheckCanExecute(); }