Пример #1
0
        private void SetForwardProperty(ForwardKeyInfo forwardKeyInfo, object newValue)
        {
            var destProperty = dataSourceGrid.SelectedItem.GetType().GetProperty(forwardKeyInfo.PropName);

            if (destProperty == null)
            {
                return;
            }
            foreach (var keyProperty in forwardKeyInfo.TargetType.GetProperties())
            {
                var attrs = keyProperty.GetCustomAttributes(typeof(KeyAttribute));
                foreach (var attr in attrs)
                {
                    var uiProperty = forwardKeyInfo.UiType.GetProperty(keyProperty.Name);
                    var keyValue   = uiProperty.GetValue(newValue);
                    destProperty.SetValue(dataSourceGrid.SelectedItem, keyValue);
#warning will only work on single-column key
                    return;
                }
            }

            Debug.Assert(false, "No primary key found");
        }
Пример #2
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");
        }