Пример #1
0
        public static LogicalUiElementType DetermineElementType(DisplayPropertyInfo property)
        {
            if (property.PropertyDataType.IsEnum)
            {
                if (property.PropertyDataType.GetEnumNames().Length <= 3)
                {
                    return LogicalUiElementType.Options;
                }
                else
                {
                    return LogicalUiElementType.ComboBox;
                }
            }

            if (property.PropertyDataType == typeof(DateTime)
                && property.LogicalDataType == LogicalDataType.Date)
            {
                return LogicalUiElementType.Date;
            }

            if (property.PropertyDataType == typeof(Boolean))
            {
                return LogicalUiElementType.Boolean;
            }

            return LogicalUiElementType.Textbox;
        }
Пример #2
0
        public static DependencyProperty GetDataGridColumnDependencyProperty(DisplayPropertyInfo property, DependencyObject depObject)
        {
            DependencyProperty dp = null;
            if (property.LogicalDependencyProperty == LogicalDependencyProperty.Default)
            {
                PropertyInfo dpPropertyInfo = depObject.GetType().GetProperty(property.DependencyPropertyName, BindingFlags.Static);
                dp = dpPropertyInfo.GetValue(null) as DependencyProperty;
            }
            else if (property.LogicalDependencyProperty == LogicalDependencyProperty.IsEnabled)
            {
                dp = DataGridColumn.IsReadOnlyProperty;
            }
            else if (property.LogicalDependencyProperty == LogicalDependencyProperty.IsVisible)
            {
                dp = DataGridColumn.VisibilityProperty;
            }
            else if (property.LogicalDependencyProperty == LogicalDependencyProperty.IsReadOnly)
            {
                dp = DataGridColumn.IsReadOnlyProperty;
            }

            if (dp == null)
            {
                LoggerFactory.GetLogger().Warn("The dependency property of property '{}' cannot be found.", property.PropertyName);
            }

            return dp;
        }
Пример #3
0
        /// <summary>
        /// Configure the specific property hidden value.
        /// * If the column name is "{EntityName}Id"
        /// * And this is self increment field.
        /// * Set the hidden as true.
        /// </summary>
        /// <param name="context"></param>
        public void Apply(EntityAnalysisContext context, DisplayPropertyInfo property)
        {
            if (EntityAnalysisHelper.IsCharacteristicProperty(context.EntityType,
                context.PropertyInfo,
                "id"))
            {
                property.IsHidden = true;
                return;
            }

            // TODO: refatory it as a plugin
            var databaseGeneratedAttribute = context.PropertyInfo.GetCustomAttribute<DatabaseGeneratedAttribute>();
            if (databaseGeneratedAttribute != null
                && databaseGeneratedAttribute.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)
            {
                property.IsHidden = true;
                return;
            }

            var timestampAttribute = context.PropertyInfo.GetCustomAttribute<TimestampAttribute>();
            if (timestampAttribute != null)
            {
                property.IsHidden = true;
                return;
            }
        }
Пример #4
0
 /// <summary>
 /// Configure the specific property hidden value.
 /// * If the column name is "{EntityName}Id"
 /// * And this is self increment field.
 /// * Set the hidden as true.
 /// </summary>
 /// <param name="context"></param>
 public void Apply(EntityAnalysisContext context, DisplayPropertyInfo property)
 {
     string[] dateTypeColumnSuffixes = new string[] { "Date", "Day" };
     foreach (var suffix in dateTypeColumnSuffixes)
     {
         if (context.PropertyInfo.Name.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
         {
             property.LogicalDataType = LogicalDataType.Date;
             return;
         }
     }
 }
Пример #5
0
        // TODO: Enrich the class PropertyControlCreator
        public static ModelPropertyUiInfo Create(DisplayPropertyInfo property,
            Grid parent,
            int row,
            int column,
            string bindingPathPrefix,
            Style style)
        {
            LogicalUiElementType elementType = ModelUiCreatorHelper.DetermineElementType(property);
            if (elementType == LogicalUiElementType.Boolean)
            {
                return CreateBooleanField(parent,
                          property,
                          bindingPathPrefix + "/" + property.PropertyName,
                          null,
                          row,
                          column);
            }
            else if (elementType == LogicalUiElementType.ComboBox)
            {
                return CreateComboBoxField(parent,
                          property,
                          bindingPathPrefix + "/" + property.PropertyName,
                          null,
                          row,
                          column);
            }
            else if(elementType == LogicalUiElementType.Date)
            {
                return CreateDateField(parent,
                          property,
                          bindingPathPrefix + "/" + property.PropertyName,
                          null,
                          row,
                          column);
            }
            else if (elementType == LogicalUiElementType.Options)
            {
                return CreateEnumField(parent,
                            property.PropertyInfo.PropertyType,
                            property,
                            bindingPathPrefix + "/" + property.PropertyName,
                            style,
                            row,
                            column);
            }

            return CreateTextBoxField(parent,
                        property,
                        bindingPathPrefix + "/" + property.PropertyName,
                        style,
                        row,
                        column);
        }
Пример #6
0
        internal static void CreateDependencyProperty(ModelPropertyUiInfo uiElementsInfo,
            DisplayPropertyInfo property,
            string bindingPathPrefix)
        {
            DependencyProperty dp = ModelUiCreatorHelper.GetDependencyProperty(property, uiElementsInfo.Content);

            if (dp == null)
            {
                return;
            }

            (uiElementsInfo.Content as FrameworkElement).SetBinding(dp,
                ModelUiCreatorHelper.CreateBinding(property, bindingPathPrefix + "/" + property.PropertyName));
        }
        public void Apply(EntityAnalysisContext context, DisplayPropertyInfo property)
        {
            var propertyName = property.PropertyName;
            int underscorePosition = propertyName.LastIndexOf('_');
            if (underscorePosition <= 0) return;

            var prefix = propertyName.Substring(0, underscorePosition);
            var suffix = propertyName.Substring(underscorePosition + 1);
            LogicalDependencyProperty logicalDependencyProperty;
            if (string.Equals(suffix, 
                                LogicalDependencyProperty.IsEnabled.ToString(),
                                StringComparison.OrdinalIgnoreCase))
            {
                logicalDependencyProperty = LogicalDependencyProperty.IsEnabled;
            }
            else if (string.Equals(suffix,
                                LogicalDependencyProperty.IsVisible.ToString(),
                                StringComparison.OrdinalIgnoreCase))
            {
                logicalDependencyProperty = LogicalDependencyProperty.IsReadOnly;
            }
            else if (string.Equals(suffix,
                                LogicalDependencyProperty.IsReadOnly.ToString(),
                                StringComparison.OrdinalIgnoreCase))
            {
                logicalDependencyProperty = LogicalDependencyProperty.IsReadOnly;
            }
            else
            {
                return;
            }

            
            var findProperty = context.EntityType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .First<PropertyInfo>(
                    (p) => string.Equals(p.Name, prefix, StringComparison.OrdinalIgnoreCase));
            if (findProperty != null)
            {
                property.DependencyHostPropertyName = findProperty.Name;
                property.LogicalDependencyProperty = logicalDependencyProperty;
                property.IsDependencyProperty = true;
            }
        }
Пример #8
0
 public static void ValidateBinding(DisplayPropertyInfo property, Binding binding, string bindingPath, string message)
 {
     Assert.AreEqual(bindingPath, binding.Path.Path, message);
     Assert.AreEqual(property.IsReadOnly
             ? BindingMode.OneWay
             : BindingMode.TwoWay,
             binding.Mode, message);
     if (!string.IsNullOrEmpty(property.ConverterTypeName))
     {
         Assert.AreEqual(property.ConverterTypeName, binding.Converter.GetType().FullName, message);
     }
     if (!string.IsNullOrEmpty(property.DisplayFormat))
     {
         Assert.AreEqual(property.DisplayFormat, binding.StringFormat, message);
     }
     if (ModelUiCreatorHelper.IsNullable(property.PropertyDataType))
     {
         Assert.IsNotNull(binding.TargetNullValue, message);
     }
 }
Пример #9
0
 private static void SetInputBehavior(DisplayPropertyInfo property, DependencyObject element)
 {
     // TODO: Enhancement it to support nullable, Uxxx, decimal etc.
     Type propertyType = property.PropertyInfo.PropertyType;
     if (ModelUiCreatorHelper.IsNumericType(propertyType))
     {
         element.SetValue(DigitsOnlyBehavior.IsDigitOnlyProperty, true);
     }
 }
Пример #10
0
        private static ModelPropertyUiInfo CreateTextBoxField(Grid parent,
            DisplayPropertyInfo property,
            String bindingPath,
            Style style,
            int row,
            int column)
        {
            Label labelElement = CreateLabel(property, row, column);

            TextBox textBox = new TextBox
            {
                Name = "textBox" + property.PropertyName,
            };
            if (style != null)
            {
                textBox.Style = style;
            }
            textBox.SetBinding(TextBox.TextProperty, ModelUiCreatorHelper.CreateBinding(property, bindingPath));

            if (property.IsReadOnly)
            {
                textBox.IsReadOnly = true;
            }

            SetInputBehavior(property, textBox);

            if (ModelUiCreatorHelper.IsNumericType(property.PropertyDataType))
            {
                textBox.HorizontalContentAlignment = HorizontalAlignment.Right;
            }
            // display caret when readonly 
            textBox.IsReadOnlyCaretVisible = true;

            Grid.SetRow(textBox, row);
            Grid.SetColumn(textBox, checked(column + 1));

            parent.Children.Add(labelElement);
            parent.Children.Add(textBox);

            // return
            ModelPropertyUiInfo elememtsInfo = new ModelPropertyUiInfo(property);
            elememtsInfo.Label = labelElement;
            elememtsInfo.Content = textBox;
            return elememtsInfo;
        }
Пример #11
0
 private static Label CreateLabel(DisplayPropertyInfo property, int row, int column)
 {
     Label labelElement = new Label
     {
         Name = "label" + property.PropertyName,
         //TODO: localization ":"
         Content = property.Name + ":"
     };
     Grid.SetRow(labelElement, row);
     Grid.SetColumn(labelElement, column);
     return labelElement;
 }
Пример #12
0
        private static ModelPropertyUiInfo CreateEnumField(Grid parent,
            Type enumType,
            DisplayPropertyInfo property,
            String bindingPath,
            Style style,
            int row,
            int column)
        {
            ModelPropertyUiInfo elememtsInfo = new ModelPropertyUiInfo(property);

            // create label
            Label labelElement = CreateLabel(property, row, column);
            parent.Children.Add(labelElement);
            elememtsInfo.Label = labelElement;

            // create inputs
            var panel = new WrapPanel()
            {
                Orientation = Orientation.Horizontal,
                Margin = new Thickness(4),
            };
            Grid.SetRow(panel, row);
            Grid.SetColumn(panel, checked(column + 1));
            parent.Children.Add(panel);
            elememtsInfo.Content = panel;

            if (!enumType.IsEnum) return elememtsInfo;
            Array enumValues = Enum.GetValues(enumType);
            foreach (var item in enumValues)
            {
                RadioButton control = new RadioButton
                {
                    Name = "radioButton" + property.PropertyName + "_" + item.ToString(),
                    // TODO: Localization for enum values
                    Content = item.ToString(),
                    GroupName = property.PropertyName,
                    Margin = new Thickness(4),
                };
                if (style != null)
                {
                    control.Style = style;
                }
                control.SetBinding(ToggleButton.IsCheckedProperty,
                    ModelUiCreatorHelper.CreateBinding(property, bindingPath, new EnumToBooleanConverter(), item.ToString()));

                if (property.IsReadOnly)
                {
                    control.IsEnabled = false;
                }

                panel.Children.Add(control);
            }

            return elememtsInfo;
        }
Пример #13
0
        private static ModelPropertyUiInfo CreateDateField(Grid grid,
            DisplayPropertyInfo property,
            String bindingPath,
            Style style,
            int row,
            int column)
        {
            Label labelElement = CreateLabel(property, row, column);

            DatePicker control = new DatePicker
            {
                Name = "datePicker" + property.PropertyName
            };
            if (style != null)
            {
                control.Style = style;
            }

            Binding binding = ModelUiCreatorHelper.CreateBinding(property, bindingPath);
            control.SetBinding(DatePicker.SelectedDateProperty, binding);

            if (property.IsReadOnly)
            {
                control.IsEnabled = false;
            }

            Grid.SetRow(control, row);
            Grid.SetColumn(control, checked(column + 1));

            grid.Children.Add(labelElement);
            grid.Children.Add(control);

            // return
            ModelPropertyUiInfo elememtsInfo = new ModelPropertyUiInfo(property);
            elememtsInfo.Label = labelElement;
            elememtsInfo.Content = control;
            return elememtsInfo;
        }
Пример #14
0
        private static ModelPropertyUiInfo CreateComboBoxField(Grid parent,
            DisplayPropertyInfo property,
            String bindingPath,
            Style style,
            int row,
            int column)
        {
            Label labelElement = CreateLabel(property, row, column);

            var comboBox = new ComboBox
            {
                Name = "comboBox" + property.PropertyName,
            };
            if (style != null)
            {
                comboBox.Style = style;
            }
            comboBox.SetBinding(Selector.SelectedItemProperty, ModelUiCreatorHelper.CreateBinding(property, bindingPath));

            if (property.IsReadOnly)
            {
                comboBox.IsReadOnly = true;
            }

            ObjectDataProvider provider = new ObjectDataProvider()
            {
                ObjectType = typeof(Enum),
                MethodName = "GetValues",
            };
            provider.MethodParameters.Add(property.PropertyInfo.PropertyType);

            BindingOperations.SetBinding(comboBox, ItemsControl.ItemsSourceProperty, new Binding()
            {
                Source = provider
            });

            Grid.SetRow(comboBox, row);
            Grid.SetColumn(comboBox, checked(column + 1));

            parent.Children.Add(labelElement);
            parent.Children.Add(comboBox);

            // return
            ModelPropertyUiInfo elememtsInfo = new ModelPropertyUiInfo(property);
            elememtsInfo.Label = labelElement;
            elememtsInfo.Content = comboBox;
            return elememtsInfo;
        }
Пример #15
0
 internal static Binding CreateBinding(DisplayPropertyInfo property)
 {
     return CreateBinding(property, property.PropertyName);
 }
Пример #16
0
 public static void ValidateBinding(DisplayPropertyInfo property, Binding binding, string message)
 {
     ValidateBinding(property, binding, property.PropertyName, message);
 }
Пример #17
0
 public ModelPropertyUiInfo(DisplayPropertyInfo displayProperty)
 {
     DisplayProperty = displayProperty;
 }
Пример #18
0
        internal static Binding CreateBinding(DisplayPropertyInfo property, string bindingPath, IValueConverter converter, string convertParameter)
        {
            var binding = new Binding(bindingPath)
            {
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                Mode = property.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay,
                ValidatesOnDataErrors = true,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true,
            };

            if (converter != null)
            {
                binding.Converter = converter;
                binding.ConverterParameter = convertParameter;
            }

            if (!string.IsNullOrEmpty(property.DisplayFormat))
            {
                binding.StringFormat = property.DisplayFormat;
            }

            if (ModelUiCreatorHelper.IsNullable(property.PropertyDataType))
            {
                binding.TargetNullValue = "";
            }

            return binding;
        }
Пример #19
0
        private static ModelPropertyUiInfo CreateBooleanField(Grid parent,
                    DisplayPropertyInfo property,
            String bindingPath,
            Style style,
            int row,
            int column)
        {
            Label labelElement = CreateLabel(property, row, column);

            var checkBox = new CheckBox
            {
                Name = "checkBox" + property.PropertyName,
            };
            if (style != null)
            {
                checkBox.Style = style;
            }
            checkBox.VerticalAlignment = VerticalAlignment.Center;
            checkBox.SetBinding(ToggleButton.IsCheckedProperty, ModelUiCreatorHelper.CreateBinding(property, bindingPath));

            if (property.IsReadOnly)
            {
                checkBox.IsEnabled = false;
            }

            Grid.SetRow(checkBox, row);
            Grid.SetColumn(checkBox, checked(column + 1));

            parent.Children.Add(labelElement);
            parent.Children.Add(checkBox);

            // return
            ModelPropertyUiInfo elememtsInfo = new ModelPropertyUiInfo(property);
            elememtsInfo.Label = labelElement;
            elememtsInfo.Content = checkBox;
            return elememtsInfo;
        }
Пример #20
0
        internal static Binding CreateBinding(DisplayPropertyInfo property, string bindingPath)
        {
            IValueConverter converter = null;
            if (!string.IsNullOrEmpty(property.ConverterTypeName))
            {
                converter = Activator.CreateInstance(Type.GetType(property.ConverterTypeName)) as IValueConverter;
            }

            return CreateBinding(property, bindingPath, converter, null);
        }