控件類是指與用戶有交互作用的控件也切,例如文本框徽诲、按鈕等
設(shè)置控件背景顏色
首先在界面定義一個(gè)按鈕
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="btn1" Padding="10" Content="Hello" Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
從后端設(shè)置按鈕的背景顏色:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_CODE
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//在窗體加載的時(shí)候設(shè)置按鈕的背景
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Background為背景屬性
//SolidColorBrush是一個(gè)實(shí)線的畫刷類
//Colors類中內(nèi)置多種顏色值
this.btn1.Background = new SolidColorBrush(Colors.Green);
}
}
}
效果:
通過XMAL設(shè)置:
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<!--通過Background來設(shè)置背景-->
<Button Name="btn1" Padding="10" Background="Red" Content="Hello" Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
效果:
在XMAL中設(shè)置背景不需要運(yùn)行程序即可看到
設(shè)置控件前景色
從后端寫:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_CODE
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//在窗體加載的時(shí)候設(shè)置按鈕的背景
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Background為背景屬性
//SolidColorBrush是一個(gè)實(shí)線的畫刷類
//Colors類中內(nèi)置多種顏色值
//this.btn1.Background = new SolidColorBrush(Colors.Green);
//Foreground為前景色,即字體的顏色
//SystemColors為系統(tǒng)里的顏色選擇器(這是另一個(gè)選擇)
this.btn1.Foreground = new SolidColorBrush(SystemColors.HighlightColor);
}
}
}
效果:
從前端寫:
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<!--通過Foreground來設(shè)置前景-->
<Button Name="btn1" Padding="10" Foreground="Red" Content="Hello" Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
效果:
使用RGB設(shè)定顏色
示例:Color.FromRgb(255, 0, 0)
this.btn1.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));
RGB表示紅綠藍(lán)三個(gè)顏色值,第個(gè)值的范圍是0~255
設(shè)置字體樣式
字體常用的屬性有:
- FontFamily - 字體家族
- FontSize - 字體大小
- FontStyle - 字體樣式
- FontWeight - 字體粗細(xì)
示例:
FontFamily="宋體, Arial, Arvo"
多個(gè)字體名稱可以用逗號隔開彼妻,從第一個(gè)開始匹配
FontSize="15"
設(shè)置字體大小
`` FontStyle="Italic" ` 設(shè)置字體為斜體
FontWeight="Bold"
設(shè)置字體不粗體
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Button Name="btn1" Padding="10" FontFamily="宋體, Arial, Arvo" FontSize="15" FontStyle="Italic" FontWeight="Bold" Background="Green" Foreground="Red" Content="Hello" Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
效果:
如果不知道系統(tǒng)上的字體都有哪些絮重,可以通過Font.SystemFontFamilies
來獲取系統(tǒng)上的字體集合
List<object> list = new List<object>();
foreach (var item in Fonts.SystemFontFamilies)
{
list.Add(item.Source);
}
為文字添加下劃線轿秧,相關(guān)屬性TextDecorations
示例:
<TextBox TextDecorations="underline">Hello</TextBox>
該文本框的里的文字將現(xiàn)出一條下劃線
將外部字體文件添加到代碼中
字體文件的后綴為.ttf钙姊,將字體文件加載到項(xiàng)目中桥嗤,然后通過路徑將其賦值給FontFamily屬性
示例:
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<TextBox TextDecorations="underline">Hello</TextBox>
<Button Name="btn1" Padding="10" FontFamily="./#Streamster" FontSize="15" FontStyle="Italic" FontWeight="Bold" Background="Green" Foreground="Red" Content="Hello" Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
FontFamily="./#Streamster"
文件名不需要加后綴须妻,但是文件名前需要有#
符號,這點(diǎn)需要注意
效果:
WPF對于小字體呈現(xiàn)不美觀
如果字體小于15泛领,將會出現(xiàn)鋸齒邊源
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" FontSize="12">Hello, my name is Jack!</TextBox>
<TextBlock Grid.Row="1" FontSize="20">Where am I?</TextBlock>
</Grid>
</Window>
可以看出荒吏,小字體明顯不如大字體清晰
可以通過TextOptions.TextFormattingMode
屬性來進(jìn)行設(shè)置小字體搞鋸齒效果
示例:
<Window x:Class="WPF_CODE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" FontSize="12" TextOptions.TextFormattingMode="Display">Hello, my name is Jack!</TextBox>
<TextBlock Grid.Row="1" FontSize="12">Where am I?</TextBlock>
</Grid>
</Window>
效果:
可以看出,同樣大小的字體渊鞋,設(shè)置過TextOptions.TextFormattingMode="Display"
屬性的字體會更清晰一點(diǎn)
需要注意的是绰更,此屬性對于大于15的字體效果不佳