WPF(三)控件2.背景画刷和前景画刷
所有控件都包含背景和前景属性,设置Background和Foreground的值改变背景或前景颜色。
1.使用代码设置颜色
button1.Background = new SolidColorBrush(Colors.Blue);
button2.Background = new SolidColorBrush(SystemColors.ControlColor);
//button2.Background = SystemColors.ControlBrush;
//button2.Background = new SolidColorBrush(Color.FromRgb(0,255,0));
2.XAML中设置颜色
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="59,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="156,12,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
<Button Content="Button" Background="Red" Height="23" HorizontalAlignment="Left" Margin="59,68,0,0" Name="button3" VerticalAlignment="Top" Width="75" />
<Button Content="Button" Height="23" HorizontalAlignment="Right" Margin="0,68,107,0" Name="button4" VerticalAlignment="Top" Width="75" Click="button4_Click">
<Button.Background>
<SolidColorBrush Color="Blue"/>
</Button.Background>
</Button>
</Grid>