问题描述
我如何使用文本coz更改某物的颜色我知道此代码
how do i change the colour of something using text coz i know this code
this.comboBox1.Background = new SolidColorBrush(Colors.DarkBlue);
我想做这个事
this.comboBox1.Background = new SolidColorBrush(comboBox1.selecteditem);
and i wanna do thing this this
this.comboBox1.Background = new SolidColorBrush(comboBox1.selecteditem);
推荐答案
public Color GetColorFromText(string colorString)
{
Type colorType = (typeof(System.Windows.Media.Colors));
if (colorType.GetProperty(colorString) != null)
{
object colour = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);
if (colour != null) ;
{
return (Color)colour;
}
}
return Color.FromRgb(255,255,255);
}
那么你可以这样称呼
then you can call it like this
this.comboBox1.Background = new SolidColorBrush(GetColorFromText("Blue"));
希望这对您有帮助
Hope this helps
<ComboBox Height="23"
Name="comboBox1"
Background="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}"/>
使用SelectedItem
属性,并将RelativeSource设置为self(其组合框为"combobox1").如果您想从另一个组合框设置背景,只需执行以下绑定即可:{Binding Path=SelectedItem, ElementName=comboBox2}
是不是很简单!
Use the SelectedItem
property with the RelativeSource set as self(its the same combobox "combobox1"). Had it been the case where you wanted to set the background from another combobox, just do this binding: {Binding Path=SelectedItem, ElementName=comboBox2}
Simple isn''t it!
这篇关于C#WPF使用文本更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!