Код: Выделить всё
//Main Style
Style style = new Style() { TargetType = typeof(ComboBoxItem) };
//Set Template
ControlTemplate temp = new ControlTemplate(typeof(ComboBoxItem));
style.Setters.Add(new Setter() { Property = Control.TemplateProperty, Value = temp });
//Item Border
FrameworkElementFactory itemB = new FrameworkElementFactory(typeof(Border));
itemB.SetValue(Border.BorderBrushProperty, Brushes.Red);
itemB.SetValue(Border.WidthProperty, (double)500);
itemB.SetValue(Border.HeightProperty, (double)30);
itemB.SetValue(Border.BorderThicknessProperty, new Thickness(2));
itemB.SetValue(Border.BackgroundProperty, Brushes.GreenYellow);
temp.VisualTree = itemB;
//Item Label
FrameworkElementFactory itemT = new FrameworkElementFactory(typeof(Label));
itemT.AppendChild(itemB);
itemT.SetValue(Label.ForegroundProperty, Brushes.Red);
/* This Part Is Not Working*/
itemT.SetBinding(Label.ContentProperty, new Binding { RelativeSource = RelativeSource.Self });
//Set Template
Application.Current.Resources[typeof(ComboBoxItem)] = style;
Код: Выделить всё
itemT.SetBinding(Label.ContentProperty, new Binding { Source = ComboBoxItem.ContentProperty });
Вот как я добавляю поле со списком
Код: Выделить всё
//ComboBox
ComboBox combobox = new ComboBox() {Height = 30, Width = 500};
combobox.Items.Add("OK 1");
combobox.Items.Add("OK 2");
combobox.Items.Add("OK 3");
combobox.Items.Add("OK 4");
[img]https://i.sstatic.net /Tyk3THJj.png[/img]
Подробнее здесь: https://stackoverflow.com/questions/786 ... -sharp-wpf
Мобильная версия