示例#1
0
        public override void AdaptTo(PropertyInfo property)
        {
            base.AdaptTo(property);
            if (property.PropertyType.IsEnum)
            {
                ItemsSource = Enum.GetValues(property.PropertyType);
            }
            else
            {
                AutoUIComboBoxAttribute cBA = property.GetCustomAttribute <AutoUIComboBoxAttribute>();

                if (cBA != null)
                {
                    if (string.IsNullOrWhiteSpace(cBA.ExtraItemsSource))
                    {
                        SetBinding(ItemsSourceProperty, cBA.ItemsSource);
                    }
                    else
                    {
                        // Bind to both the standard and extra item sources
                        ItemsSource = cBA.GetCombinedSourceCollection(this);
                    }
                }
                ItemTemplate = new DataTemplate();
                FrameworkElementFactory fEFactory = new FrameworkElementFactory(typeof(TextBlock));

                if (typeof(INamed).IsAssignableFrom(property.PropertyType))
                {
                    string textTemplate = "Name";
                    fEFactory.SetBinding(TextBlock.TextProperty, new Binding(textTemplate));
                }
                else
                {
                    if (typeof(Colour).IsAssignableFrom(property.PropertyType))
                    {
                        fEFactory = new FrameworkElementFactory(typeof(Border));

                        Binding binding = new Binding();
                        binding.Converter = new Converters.BrushConverter();
                        fEFactory.SetBinding(Border.BackgroundProperty, binding);
                        fEFactory.SetValue(Border.HeightProperty, 16d);
                        fEFactory.SetValue(Border.WidthProperty, 16d);
                    }
                    else
                    {
                        fEFactory.SetBinding(TextBlock.TextProperty, new Binding());
                    }
                }

                ItemTemplate.VisualTree = fEFactory;
            }
        }
        /// <summary>
        /// Get the compositecollection detailing the full set of available source items
        /// specified by this attribute
        /// </summary>
        /// <param name="cBA"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static CompositeCollection GetCombinedSourceCollection(this AutoUIComboBoxAttribute cBA, object source)
        {
            CompositeCollection collection = new CompositeCollection();
            var c1 = new CollectionContainer();
            var b1 = new Binding("DataContext." + cBA.ItemsSource);

            b1.Source = source;
            BindingOperations.SetBinding(c1, CollectionContainer.CollectionProperty, b1);
            collection.Add(c1);

            var c2 = new CollectionContainer();
            var b2 = new Binding("DataContext." + cBA.ExtraItemsSource);

            b2.Source = source;
            BindingOperations.SetBinding(c2, CollectionContainer.CollectionProperty, b2);
            collection.Add(c2);

            return(collection);
        }
示例#3
0
        protected void GenerateAutoUIColumns(IList <PropertyInfo> properties)
        {
            Columns.Clear(); // Clear previous columns
            // TODO: Keep manually-added columns?
            Model.Model model = null;
            if (ItemsSource != null && ItemsSource is IOwned <Model.Model> )
            {
                model = ((IOwned <Model.Model>)ItemsSource).Owner;
            }

            foreach (PropertyInfo p in properties)
            {
                PropertyInfo    property = p;
                Binding         binding  = null;
                AutoUIAttribute aUI      = property.GetCustomAttribute <AutoUIAttribute>();
                if (aUI.SubProperty != null)
                {
                    // Switch to displaying sub-property
                    binding  = new Binding(property.Name + "." + aUI.SubProperty);
                    property = property.PropertyType.GetProperty(aUI.SubProperty);
                    aUI      = property.GetCustomAttribute <AutoUIAttribute>();
                }
                else
                {
                    binding = new Binding(property.Name);
                }
                binding.Converter = new TextConverter(model);
                if (!property.CanWrite)
                {
                    binding.Mode = BindingMode.OneWay;
                }
                DataGridColumn column;

                if (property.HasAttribute(typeof(AutoUIComboBoxAttribute)))
                {
                    AutoUIComboBoxAttribute cBA = property.GetCustomAttribute <AutoUIComboBoxAttribute>();

                    /*var comboColumn = new DataGridComboBoxColumn();
                     * column = comboColumn;
                     * comboColumn.SelectedItemBinding = binding;*/

                    /*BindingOperations.SetBinding(comboColumn, DataGridComboBoxColumn.ItemsSourceProperty,
                     *  sourceBinding);*/

                    var comboColumn = new DataGridTemplateColumn();
                    column = comboColumn;

                    var cellTemplate = new DataTemplate();
                    var tbFactory    = new FrameworkElementFactory(typeof(TextBlock));
                    tbFactory.SetBinding(TextBlock.TextProperty, binding);
                    //tbFactory.SetValue(TextBlock.PaddingProperty, new Thickness(2.0,2.0,2.0,2.0));
                    cellTemplate.VisualTree  = tbFactory;
                    comboColumn.CellTemplate = cellTemplate;

                    var editTemplate = new DataTemplate();
                    var cbFactory    = new FrameworkElementFactory(typeof(ComboBox));
                    cbFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true);
                    cbFactory.SetValue(ComboBox.IsEditableProperty, true);
                    cbFactory.SetValue(ComboBox.PaddingProperty, new Thickness(2.0, 0.0, 2.0, 0.0));
                    //cbFactory.SetBinding(ComboBox.TextProperty, binding);

                    // Set ItemsSource binding:
                    Binding sourceBinding;
                    if (!string.IsNullOrEmpty(cBA.ItemsSource))
                    {
                        sourceBinding      = new Binding();
                        sourceBinding.Path = new PropertyPath(cBA.ItemsSource);
                    }
                    else
                    {
                        sourceBinding = new Binding();
                        if (property.PropertyType.IsEnum)
                        {
                            sourceBinding.Source = Enum.GetValues(property.PropertyType);
                        }
                        else if (typeof(Family).IsAssignableFrom(property.PropertyType))
                        {
                            sourceBinding.Path = new PropertyPath("Model.Families");
                        }
                        else if (typeof(CoordinateSystemReference).IsAssignableFrom(property.PropertyType))
                        {
                            // TEMP: Should be updated to include custom coordinate systems!
                            var pInfo = typeof(CoordinateSystemReference).GetProperty("StandardValues");
                            sourceBinding.Path = new PropertyPath("(0)", pInfo);
                            //cbFactory.SetValue(ComboBox.ItemsSourceProperty, CoordinateSystemReference.StandardValues);
                        }
                        //sourceBinding.Converter = new ModelTableConverter();
                        //sourceBinding.ConverterParameter = property.PropertyType;
                    }

                    cbFactory.SetBinding(ComboBox.SelectedItemProperty, new Binding(property.Name));
                    cbFactory.SetBinding(ComboBox.ItemsSourceProperty, sourceBinding);

                    editTemplate.VisualTree         = cbFactory;
                    comboColumn.CellEditingTemplate = editTemplate;
                }
                else
                {
                    var textColumn = new DataGridTextColumn();
                    column             = textColumn;
                    textColumn.Binding = binding;
                }
                if (aUI?.Label != null)
                {
                    column.Header = aUI.Label;
                }
                else
                {
                    column.Header = p.Name.AutoSpace();
                }

                column.MinWidth = 100;

                Columns.Add(column);
            }
        }