我有一个ComboBox,我想将边框的颜色更改为红色,以将其标记为必填字段。我通过我的代码动态生成这个ComboBox列。为了更改边框颜色,我应该设置哪些属性?
DataGridTemplateColumn dataGridComboBoxTemplateColumnObj = new DataGridTemplateColumn();
dataGridComboBoxTemplateColumnObj.Header = column.Header;
FrameworkElementFactory comboBoxFactory = new FrameworkElementFactory(typeof(ComboBox));
Binding bindingItemSourceObj = new Binding(column.ItemsSourcePropertyName);
comboBoxFactory.SetValue(ComboBox.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
comboBoxFactory.SetValue(ComboBox.ItemsSourceProperty, bindingItemSourceObj);
comboBoxFactory.SetValue(ComboBox.SelectedValuePathProperty, column.ValuePropertyName);
Binding selectedValueBindingObj = new Binding(column.PropertyName);
selectedValueBindingObj.Mode = BindingMode.TwoWay;
selectedValueBindingObj.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
comboBoxFactory.SetValue(ComboBox.SelectedValueProperty, selectedValueBindingObj);
dataGridComboBoxTemplateColumnObj.CellTemplate = new DataTemplate();
dataGridComboBoxTemplateColumnObj.CellTemplate.VisualTree = comboBoxFactory;
dataGridColumnObj = dataGridComboBoxTemplateColumnObj;
答案 0 :(得分:2)
如果您有ComboBox
:
<ComboBox x:Name="MyComboBox"/>
您只需设置Border.BorderBrushProperty
:
MyComboBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
通过这种方式,您可以准确地将ComboBox
的边框颜色更改为红色。
修改强>:
我为您的特定FrameworkElementFactory
添加了此示例:
FrameworkElementFactory comboBoxFactory = new FrameworkElementFactory(typeof(ComboBox));
comboBoxFactory.SetValue(Border.BorderBrushProperty, Brushes.Red);
ControlTemplate ct = new ControlTemplate(typeof(ComboBox));
ct.VisualTree = comboBoxFactory;
MyComboBox.Template = ct;