예제 #1
0
        private void AddItem(string name, object value, string bindingName)
        {
            Func <ComboBoxItem, bool> predicate = null;
            var type    = value.GetType();
            var element = new Grid {
                Margin = new Thickness(0.0, 0.0, 0.0, 10.0)
            };
            var definition4 = new ColumnDefinition {
                Width = new GridLength(126.0)
            };

            element.ColumnDefinitions.Add(definition4);
            var definition5 = new ColumnDefinition {
                Width = new GridLength(1.0, GridUnitType.Star)
            };

            element.ColumnDefinitions.Add(definition5);
            var block = new TextBlock {
                Text       = name,
                Foreground = Brushes.White
            };

            element.Children.Add(block);
            if (((type == typeof(double)) || (type == typeof(int))) || ((type == typeof(float)) || (type == typeof(Thickness))))
            {
                var binding = new Binding(bindingName)
                {
                    Mode = BindingMode.TwoWay
                };
                var textBox = new TextBox();
                textBox.TextChanged += delegate {
                    foreach (var ch in textBox.Text)
                    {
                        switch (ch)
                        {
                        case ',':
                        case '.':
                            return;
                        }
                    }
                    textBox.Text = new string((
                                                  from c in textBox.Text
                                                  where char.IsDigit(c)
                                                  select c
                                                  ).ToArray());
                    textBox.SelectionStart = textBox.Text.Length;
                };
                textBox.Text = value.ToString();
                Grid.SetColumn(textBox, 1);
                textBox.SetBinding(TextBox.TextProperty, binding);
                element.Children.Add(textBox);
            }
            else if (type == typeof(SolidColorBrush))
            {
                var binding2 = new Binding(bindingName)
                {
                    Mode      = BindingMode.TwoWay,
                    Converter = Resources["brushToColorConverter"] as Legacy.BrushToColorConverter
                };
                var box = new ColorComboBox {
                    Margin = new Thickness(0.0, -4.0, 0.0, 4.0)
                };
                Grid.SetColumn(box, 1);
                box.SetBinding(ColorComboBox.SelectedColorProperty, binding2);
                element.Children.Add(box);
            }
            else
            {
                if (type == typeof(string))
                {
                    var grid2 = new Grid {
                        Margin = new Thickness(0.0, 0.0, 0.0, 10.0)
                    };
                    var definition = new ColumnDefinition {
                        Width = new GridLength(0.3, GridUnitType.Star)
                    };
                    grid2.ColumnDefinitions.Add(definition);
                    var definition2 = new ColumnDefinition {
                        Width = new GridLength(1.0, GridUnitType.Auto)
                    };
                    grid2.ColumnDefinitions.Add(definition2);
                    var definition3 = new ColumnDefinition {
                        Width = new GridLength(0.3, GridUnitType.Star)
                    };
                    grid2.ColumnDefinitions.Add(definition3);
                    var grid3 = new Grid {
                        Height     = 1.0,
                        Background = Brushes.White
                    };
                    Grid.SetColumn(grid3, 0);
                    var grid4 = new Grid {
                        Height     = 1.0,
                        Background = Brushes.White
                    };
                    Grid.SetColumn(grid4, 2);
                    var block2 = new TextBlock {
                        Text                = value.ToString(),
                        Foreground          = Brushes.White,
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment   = VerticalAlignment.Center,
                        Margin              = new Thickness(5.0, 0.0, 5.0, 0.0)
                    };
                    Grid.SetColumn(block2, 1);
                    grid2.Children.Add(grid3);
                    grid2.Children.Add(grid4);
                    grid2.Children.Add(block2);
                    spEditor.Children.Add(grid2);
                    return;
                }
                if (type == typeof(FontFamily))
                {
                    var binding3 = new Binding(bindingName)
                    {
                        Mode = BindingMode.TwoWay
                    };
                    var list  = (from f in new InstalledFontCollection().Families select new ComboBoxItem {
                        Content = f.Name, HorizontalContentAlignment = HorizontalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Stretch, FontFamily = new FontFamily(f.Name)
                    }).ToList <ComboBoxItem>();
                    var combo = new ComboBox {
                        ItemsSource = list
                    };
                    combo.SetBinding(FontFamilyProperty, binding3);
                    combo.SelectionChanged += delegate {
                        if (combo.SelectedItem != null)
                        {
                            var selectedItem = (ComboBoxItem)combo.SelectedItem;
                            combo.FontFamily = selectedItem.FontFamily;
                        }
                    };
                    predicate          = c => c.FontFamily.ToString() == value.ToString();
                    combo.SelectedItem = combo.Items.Cast <ComboBoxItem>().FirstOrDefault <ComboBoxItem>(predicate);
                    Grid.SetColumn(combo, 1);
                    element.Children.Add(combo);
                }
                else if (type == typeof(FontWeight))
                {
                    var binding4 = new Binding(bindingName)
                    {
                        Mode = BindingMode.TwoWay
                    };
                    var combo = new ComboBox {
                        ItemsSource = Resources["fontWeights"] as FontWeight[]
                    };
                    combo.SetBinding(FontWeightProperty, binding4);
                    combo.SelectionChanged += delegate {
                        if (combo.SelectedItem != null)
                        {
                            combo.FontWeight = (FontWeight)combo.SelectedItem;
                        }
                    };
                    combo.SelectedItem = (FontWeight)value;
                    Grid.SetColumn(combo, 1);
                    element.Children.Add(combo);
                }
            }
            spEditor.Children.Add(element);
        }