WPF之路-控件類

控件類是指與用戶有交互作用的控件也切,例如文本框徽诲、按鈕等

設(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的字體效果不佳

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市锡宋,隨后出現(xiàn)的幾起案子儡湾,更是在濱河造成了極大的恐慌,老刑警劉巖执俩,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盒粮,死亡現(xiàn)場離奇詭異,居然都是意外死亡奠滑,警方通過查閱死者的電腦和手機(jī)丹皱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宋税,“玉大人摊崭,你說我怎么就攤上這事〗苋” “怎么了呢簸?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我根时,道長瘦赫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任蛤迎,我火速辦了婚禮确虱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘替裆。我一直安慰自己校辩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布辆童。 她就那樣靜靜地躺著宜咒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪把鉴。 梳的紋絲不亂的頭發(fā)上故黑,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機(jī)與錄音庭砍,去河邊找鬼场晶。 笑死,一個(gè)胖子當(dāng)著我的面吹牛逗威,可吹牛的內(nèi)容都是我干的峰搪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼凯旭,長吁一口氣:“原來是場噩夢啊……” “哼概耻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起罐呼,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤鞠柄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后嫉柴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體厌杜,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年计螺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了夯尽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡登馒,死狀恐怖匙握,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情陈轿,我是刑警寧澤圈纺,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布秦忿,位于F島的核電站,受9級特大地震影響蛾娶,放射性物質(zhì)發(fā)生泄漏灯谣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一蛔琅、第九天 我趴在偏房一處隱蔽的房頂上張望胎许。 院中可真熱鬧,春花似錦揍愁、人聲如沸呐萨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至切距,卻和暖如春朽缎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背谜悟。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工话肖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人葡幸。 一個(gè)月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓最筒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蔚叨。 傳聞我的和親對象是個(gè)殘疾皇子床蜘,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容