如何改变ComboBox的边框颜色使用代码? [英] How to change Border color of ComboBox using code?
本文介绍了如何改变ComboBox的边框颜色使用代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个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;
解决方案
如果你有一个 ComboBox
:
< ComboBox x:Name =MyComboBox/>
您可以设置 Border.BorderBrushProperty
:
MyComboBox.SetValue(Border.BorderBrushProperty,Brushes.Red);
以这种方式,您可以更改 ComboBox $ <$> :
我为您的特定 FrameworkElementFactory 添加了此示例。 / code>:
FrameworkElementFactory comboBoxFactory = new FrameworkElementFactory(typeof(ComboBox));
comboBoxFactory.SetValue(Border.BorderBrushProperty,Brushes.Red);
ControlTemplate ct = new ControlTemplate(typeof(Combobox));
ct.VisualTree = comboBoxFactory;
MyComboBox.Template = ct;
I have a ComboBox and I want to change the border's color to red to mark it as a mandatory field. I am generating this ComboBox column dynamically through my Code. What are the properties that I should set in order to change the border color?
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;
解决方案
If you have a ComboBox
:
<ComboBox x:Name="MyComboBox"/>
You can just set the value of Border.BorderBrushProperty
:
MyComboBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
In this way you change exactly the border color of the ComboBox
to red.
Edit:
I added this example for your specific 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;
这篇关于如何改变ComboBox的边框颜色使用代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文