(1)代碼生成的顏色:
前台:
<TextBox x:Name="textbox1" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0 10 0 10 ">啊實打實</TextBox>
<TextBox x:Name="textbox2" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0 10 0 10 ">阿達</TextBox> <TextBox x:Name="textbox3" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0 10 0 10 ">ads打算</TextBox> <TextBox x:Name="textbox4" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0 10 0 10 ">阿達</TextBox> <TextBox x:Name="textbox5" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0 10 0 10 ">這</TextBox>
后台:
第一種創建方法:
textbox1.Background = new SolidColorBrush(Colors.Blue); //單一的畫刷
第二種創建方法
textbox2.Background = new SolidColorBrush(SystemColors.ControlColor);
或textbox2.Background = SystemColors.ControlBrush;
第三種創建方法
byte red = 0; byte green = 225; byte blue = 0;
textbox3.Background = new SolidColorBrush(Color.FromRgb(red,green,blue));
也可以這樣創建
Color color = new Color();
color.A = 225;
color.R = 201;
color.G = 0;
color.B = 0;
textbox4.Background = new SolidColorBrush(color);
第四種創建方法
textbox4.Background = new SolidColorBrush(Color.FromArgb(100, 0, 0, 0));
第五種創建方法 :這個的顏色是浮點型的,所以要轉成浮點型的。以前的255在這代表的是1.范圍是:0~1
textbox5.Background = new SolidColorBrush(Color.FromScRgb((float)1, (float)0.225, (float)0.35, (float)0.0));
(2)在XAML中設置顏色
在XAML中設置顏色是比較簡單的
例如
<TextBox x:Name="textbox6" Background="Aquamarine" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0 10 0 10 ">這</TextBox>
<TextBox x:Name="textbox7" Background="#FFFF0000" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0 10 0 10 ">這</TextBox>