private void filed_changed(object sender, SelectionChangedEventArgs e) { var controlop = new ControlAndOperator(); var pinfo = field_cb.SelectedValue as PropertyInfo; var ctlcontext = new FormItemContext(_type, pinfo); controlop.InputControl = InputControlBuilder.CreateControl(ctlcontext); if (pinfo.PropertyType.Name.Equals("string", StringComparison.OrdinalIgnoreCase)) { controlop.UseStringOperator = true; } else { controlop.UseNumberOperator = true; } if (CreateControlCallback != null) { controlop = CreateControlCallback(ctlcontext, controlop); } _bindobject = Activator.CreateInstance(_type); controlop.InputControl.DataContext = _bindobject; input_br.Child = controlop.InputControl; operator_cb.ItemsSource = null; if (controlop.UseStringOperator) { operator_cb.ItemsSource = EnumNameValuePair.EnumToList(typeof(StringOperator)); } else if (controlop.UseNumberOperator) { operator_cb.ItemsSource = EnumNameValuePair.EnumToList(typeof(NumberOperator)); } operator_cb.SelectedIndex = 0; if (pinfo.PropertyType.IsEnum) { operator_cb.IsEnabled = false; } else { operator_cb.IsEnabled = true; } }
public void RenderForm(object data, bool isreadonly) { DataContext = data; var datatype = data.GetType(); if (InputControlBuilder == null) { InputControlBuilder = new ControlsBuilder(); } root.Children.Clear(); _errlabels.Clear(); _editctls.Clear(); var props = datatype.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty); int rownum = 0; for (int i = 0; i < props.Count(); i++) { var fieldcontext = new FormItemContext(data, datatype, props[i], ControlType.None); // determine if create field bool suggestion = true; var attr0 = props[i].GetCustomAttribute(typeof(EditableAttribute)) as EditableAttribute; var attr1 = props[i].GetCustomAttribute(typeof(KeyAttribute)) as KeyAttribute; if (attr1 != null || (attr0 != null && !attr0.AllowEdit)) { suggestion = false; } if (DetermineFieldCreationCallback != null) { suggestion = DetermineFieldCreationCallback(fieldcontext, suggestion); } if (!suggestion) { continue; } // add row root.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0, GridUnitType.Auto) }); var rowcontainer = new StackPanel(); rowcontainer.Orientation = Orientation.Horizontal; Grid.SetRow(rowcontainer, rownum); root.Children.Add(rowcontainer); // label var lbcontext = new FormItemContext(data, datatype, props[i], ControlType.Label); Label label = new Label(); var attr = props[i].GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute; if (attr != null) { if (!string.IsNullOrEmpty(attr.Name)) { label.Content = attr.Name; } if (!string.IsNullOrEmpty(attr.Description)) { label.ToolTip = attr.Description; } } if (label.Content == null) { label.Content = props[i].Name; } StyleHelper.ApplyStyle(label); var labelctl = OnCreateControl(lbcontext, label); rowcontainer.Children.Add(labelctl); // editable var editcontext = new FormItemContext(data, datatype, props[i], ControlType.Editable); var editctl = InputControlBuilder.CreateControl(editcontext); editctl = OnCreateControl(editcontext, editctl); if (isreadonly) { editctl.IsEnabled = false; } rowcontainer.Children.Add(editctl); _editctls.Add(editctl); // error label var errtb = new TextBlock(); if (!isreadonly) { errtb.Tag = props[i].Name; var errbinding = new Binding { Source = editctl, Path = CustomValidation.GetValidationMsgBindingPath(editctl) }; errtb.SetBinding(TextBlock.TextProperty, errbinding); StyleHelper.ApplyStyle(errtb, FormControlConstrants.VALIDATION_ERROR_STYLE); rowcontainer.Children.Add(errtb); _errlabels.Add(errtb); } if (LayoutControlCallback != null) { LayoutControlCallback(this.root, label, editctl, errtb); } rownum++; } if (isreadonly) { ConfirmButton.Visibility = Visibility.Collapsed; } }