예제 #1
0
        private void DoBuild(Type dataType)
        {
            //theGrid.Children.Clear();
            Debug.WriteLine("DetailGridBuilder.Build(" + dataType.Name + ")");
            //var measure = new TextBox();
            //theGrid.Children.Add(measure);

            // build
            foreach (var prop in dataType.GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public))
            {
                var forwardKeyInfo = new ForwardKeyInfo {
                    PropName = prop.Name
                };
                var colName = prop.Name;
                var attrs   = prop.GetCustomAttributes(typeof(BrowsableAttribute), true);
                if (attrs.Length > 0)
                {
                    if (!(attrs[0] as BrowsableAttribute).Browsable)
                    {
                        // not browsable
                        if (typeof(IUiBase).IsAssignableFrom(dataType))
                        {
                            var originalType = dataType.GetField("original", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
                            if (originalType != null)
                            {
                                var originalProperty = originalType.FieldType.GetProperty(prop.Name);
                                if (originalProperty != null)
                                {
                                    attrs = originalProperty.GetCustomAttributes(typeof(ForeignKeyAttribute), true);
                                    if (attrs.Length > 0)
                                    {
                                        forwardKeyInfo.TargetType = typeof(IDalData).Assembly.GetType("Conta.DAL.Model." + (attrs[0] as ForeignKeyAttribute).Name);
                                        if (forwardKeyInfo.TargetType != null)
                                        {
                                            // infer ui target type
                                            foreach (var type in typeof(AppServices).Assembly.GetTypes())
                                            {
                                                var originalField = type.GetField("original", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
                                                if (originalField != null &&
                                                    forwardKeyInfo.TargetType == originalField.FieldType)
                                                {
                                                    forwardKeyInfo.UiType = type;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (forwardKeyInfo.TargetType == null)
                        {
                            continue;   // Browse(false) & not a foreign key
                        }
                    }
                }

                attrs = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true);
                if (attrs.Length > 0)
                {
                    colName = (attrs[0] as DisplayNameAttribute).DisplayName;
                }

                var isReadOnly = false;
                attrs = prop.GetCustomAttributes(typeof(ReadOnlyAttribute), true);
                if (attrs.Length > 0)
                {
                    isReadOnly = (attrs[0] as ReadOnlyAttribute).IsReadOnly;
                }
                else
                {
                    isReadOnly = !prop.CanWrite;
                }

                Control editor = null;
                if (prop.PropertyType == typeof(DateTime))
                {
                    var dt = new DatePicker {
                        Name      = "datePicker_" + prop.Name,
                        IsTabStop = true,
                        //Background = Brushes.Azure,
                    };

                    dt.SetBinding(DatePicker.SelectedDateProperty, ApplyBinding(prop.Name, isReadOnly));
                    editor = dt;
                }
                else if (forwardKeyInfo.TargetType != null)
                {
                    // "known" class : ckeck for foreign key
                    var ctrl = new Button();
                    ctrl.Content = prop.Name;
                    ctrl.Click  += ctrlForeignKey_Click;
                    ctrl.Tag     = forwardKeyInfo;
                    //ctrl.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
                    //display.EditorWidth = ctrl.ActualWidth;

                    //ctrl.SetBinding(Button.TagProperty, ApplyBinding(string.Empty, isReadOnly));
                    editor = ctrl;
                }
                else if ((attrs = prop.GetCustomAttributes(typeof(LookupBoundPropertyAttribute), true)).Length > 0)
                {
                    var lookupAttribute = attrs[0] as LookupBoundPropertyAttribute;
                    var combo           = new ComboBox {
                        Name = "comboBox_" + prop.Name,
                        DisplayMemberPath = lookupAttribute.DisplayMember,
                        SelectedValuePath = lookupAttribute.LookupMember,
                    };

                    combo.SetBinding(ComboBox.SelectedValueProperty, ApplyBinding(prop.Name, isReadOnly));
                    if (UiProjectStatus.Service == null)
                    {
                        UiProjectStatus.InitService();
                    }
                    combo.ItemsSource = UiProjectStatus.Service.GetList();

                    editor = combo;
                }
                else
                {
                    // catch-all
                    var textLen = 10;
                    //if (prop.PropertyType == typeof(int)) textLen = 10;
                    if (prop.PropertyType == typeof(string))
                    {
                        attrs = prop.GetCustomAttributes(typeof(StringLengthAttribute), true);
                        if (attrs.Length > 0)
                        {
                            textLen = (attrs[0] as StringLengthAttribute).MaximumLength;
                        }
                    }

                    var isForeignValue = prop.PropertyType.IsClass && prop.PropertyType != typeof(string);
                    isReadOnly |= isForeignValue;

                    var ctrl = new TextBox {
                        //Background = Brushes.Azure,
                        Name = "textBox_" + prop.Name,
                        //Text = new string('n', textLen),
                        IsReadOnly = isReadOnly,
                    };
                    //measure.Text = new string('n', textLen);
                    //measure.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));     --> 0
                    //ctrl.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));     --> 0
                    //display.EditorWidth = ctrl.ActualWidth;
                    //Debug.WriteLine("width: " + ctrl.ActualWidth);
                    //Debug.WriteLine("width: " + measure.ActualWidth);

                    var binding = ApplyBinding(prop.Name, isReadOnly);
                    if (isForeignValue)
                    {
                        binding.Converter = new ToStringConverter();
                    }

                    ctrl.SetBinding(TextBox.TextProperty, binding);
                    editor = ctrl;
                }

                if (string.IsNullOrEmpty(editor.Name))
                {
                    editor.Name = "detail_" + dataType.Name + "_" + prop.Name;
                }
                var display = new Record(/*dataType.Name + */ colName, editor);
                DataSource.Add(display);
                display.AddToGrid(theGrid);
            }

            //theGrid.Children.Remove(measure);

            InitGrid(theGrid);
            Debug.WriteLine("init");
            Rearrange(theGrid.ActualWidth);
            Debug.WriteLine("end init");
        }
예제 #2
0
        void theGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            //Debug.WriteLine("AutoGeneratingColumn(" + e.PropertyName + ")");
            var prop = controller.CurrentType.GetProperty(e.PropertyName);

            if (prop == null)
            {
                return;
            }

            var attrs = prop.GetCustomAttributes(typeof(BrowsableAttribute), true);

            if (attrs.Length != 0)
            {
                e.Cancel = !(attrs[0] as BrowsableAttribute).Browsable;
            }
            if (e.Cancel)
            {
                return;
            }

            attrs = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true);
            if (attrs.Length != 0)
            {
                e.Column.Header = (attrs[0] as DisplayNameAttribute).DisplayName;
            }

            //DataGridComboBoxColumn
            if (e.Column is DataGridBoundColumn)
            {
                var column = e.Column as DataGridBoundColumn;
                // combobox
                attrs = prop.GetCustomAttributes(typeof(LookupBoundPropertyAttribute), true);
                if (attrs.Length != 0)
                {
                    var lookupAttribute = attrs[0] as LookupBoundPropertyAttribute;
                    //         <ComboBox Name="combo" DisplayMemberPath="value" SelectedValuePath="key" SelectedValue="{Binding combo}" ItemsSource="{Binding items}"/>
                    var lookupColumn = new DataGridComboBoxColumn {
                        Header            = column.Header,
                        DisplayMemberPath = lookupAttribute.DisplayMember,
                        //IsAutoGenerated = true,
                        SelectedValuePath    = lookupAttribute.LookupMember,
                        SelectedValueBinding = GetBinding(e.PropertyName),
                    };
                    //lookupColumn.ItemsSource = GetService(lookupAttribute.DataSource);
                    if (UiProjectStatus.Service == null)
                    {
                        UiProjectStatus.InitService();
                    }
                    lookupColumn.ItemsSource = UiProjectStatus.Service.GetList();

                    e.Column = lookupColumn;
                    return;
                }

                // date / date/time / time
                if (e.PropertyType == typeof(DateTime))
                {
                    // datepicker
                    var dateBinder     = GetBinding(e.PropertyName);
                    var factoryElement = new FrameworkElementFactory(typeof(DatePicker));
                    factoryElement.SetValue(DatePicker.DisplayDateProperty, dateBinder);
                    factoryElement.SetValue(DatePicker.SelectedDateProperty, dateBinder);
                    var cellTemplate = new DataTemplate {
                        VisualTree = factoryElement,
                    };
                    var dateColumn = new DataGridTemplateColumn {
                        Header       = e.Column.Header,
                        CellTemplate = cellTemplate,
                    };
                    //dateColumn.Binding = GetBinding(e.PropertyName);
                    e.Column = dateColumn;
                    return;
                }

                // all others
                column.Binding = GetBinding(e.PropertyName);
            }
        }