【24】C# 自定义控件样式

目前接触到的C#应用程序,基本上采用了WPF进行界面设计,WPF是啥?是微软推出的基于Windows 的用户界面框架,利用它能够将界面设计和逻辑代码完全分离,而且能够实现很炫的画面效果,当然前提是你对它的使用较为熟悉,笔者目前也是在学习关于界面样式这方面的内容。

标签内样式

首先,我们新建一个WPF应用程序,建成之后我们可以看到主窗口的设计界面,然后我们根据自己想要的界面,进行制作,这里就使用button进行测试了。添加button按钮,点击鼠标右键,选择属性,或者按F4呼出属性界面设置窗口。对属性熟悉的可以直接修改xaml文件即可。我们设置了按钮的大小,以及颜色,这应该难不倒你,接着继续换一种方式。

<Window x:Class="teststyle.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 Content="Button" Height="37" HorizontalAlignment="Left" Margin="34,26,0,0" Name="button1" VerticalAlignment="Top" Width="85" Background="#FFE99A9A" />
    </Grid>
</Window>
2018-04-24_233932.png
2018-04-24_234005.png
使用Style

利用Style对元素进行样式设置,它就类似css可以设置html标签的样式。假如你想把按钮设置成相同的样式,你使用Style就能轻松的实现,当然,如果你想其中一个不一样也是可以的,下面用代码介绍它的使用。
这里放置了3个按钮,并用Style设置了他们的背景色,边框,透明度三个属性。
TargetType="Button" 这样样式就会作用于所有的button了。

<Window x:Class="teststyle.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">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="red"></Setter>
            <Setter Property="BorderBrush" Value="black"></Setter>
            <Setter Property="Opacity" Value="0.5"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Content="Button" Height="37" HorizontalAlignment="Left" Margin="34,26,0,0" Name="button1" VerticalAlignment="Top" Width="85"  />
        <Button Content="Button" Height="37" HorizontalAlignment="Left" Margin="211,27,0,0" Name="button2" VerticalAlignment="Top" Width="85" />
        <Button Content="Button" Height="37" HorizontalAlignment="Left" Margin="384,27,0,0" Name="button3" VerticalAlignment="Top" Width="85" />
    </Grid>
</Window>
2018-04-25_001559.png

如何让它作用于其中一个呢?需要在样式上提供x:key,然后,将样式应用到标签上Style="{StaticResource 你的key值}" ,这样你就能单独设置其样式了,是不是跟css通过id和class进行设置一样。

<Window x:Class="teststyle.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">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="red"></Setter>
            <Setter Property="BorderBrush" Value="black"></Setter>
            <Setter Property="Opacity" Value="0.5"></Setter>
        </Style>
        
        <Style x:Key="btnone" TargetType="Button">
            <Setter Property="Background" Value="yellow"></Setter>
            <Setter Property="Content" Value="btnone"></Setter>
            <Setter Property="Opacity" Value="1"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Style="{StaticResource btnone}" Height="37" HorizontalAlignment="Left" Margin="34,26,0,0" Name="button1" VerticalAlignment="Top" Width="85"  />
        <Button Content="Button" Height="37" HorizontalAlignment="Left" Margin="211,27,0,0" Name="button2" VerticalAlignment="Top" Width="85" />
        <Button Content="Button" Height="37" HorizontalAlignment="Left" Margin="384,27,0,0" Name="button3" VerticalAlignment="Top" Width="85" />
    </Grid>
</Window>
2018-04-25_001803.png
使用模板

关于模板,我们比较常用的就是控件模板(ControlTemplate)和数据模板(DataTemplate)了。通过模板你可以改变控件的结构和外观。单独使用ControlTemplate必须制定key值,你可以使用style加模板的方式,就不是必要了。

<Window x:Class="teststyle.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">
    <Window.Resources>
        <ControlTemplate x:Key="btnone" TargetType="Button">
            <Border Background="red" BorderThickness="1">
                <Grid Margin="1" Background="AliceBlue">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="4,5,4,4" />
                </Grid>
            </Border>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Button Template="{StaticResource btnone}" Content="Button" Height="37" HorizontalAlignment="Left" Margin="34,26,0,0" Name="button1" VerticalAlignment="Top" Width="85"  />
    </Grid>
</Window>
2018-04-25_004952.png
根据控件状态改变样式

这里介绍两种方式,一种是通过触发器(Triggers)来进行改变,另一种使用visualstate对象改变控件的样式,.net4.0开始引入VisualStateManager,主要为了控制控件的状态转换,和其间涉及的外观行为。,所要要使用这个必须将项目换成.net4.0框架。

//鼠标移动上去,改变字体大小
<Window x:Class="teststyle.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">
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Trigger.Setters>
                        <Setter Property="FontSize" Value="22" />
                    </Trigger.Setters>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Content="Button" Height="37" HorizontalAlignment="Left" Margin="34,26,0,0" Name="button1" VerticalAlignment="Top" Width="85"  />
    </Grid>
</Window>
2018-04-25_012121.png
//鼠标移动上去改变边框颜色
<Window x:Class="teststyle.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">
    <Window.Resources>
        <ControlTemplate x:Key="btnone" TargetType="Button">
            <Border>
                <Border.Background>
                    <SolidColorBrush x:Name="BorderBrush" Color="Black"/>
                </Border.Background>
                <Grid Background="{TemplateBinding Background}" Margin="4">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="2" />
                </Grid>
                <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                <ColorAnimation Storyboard.TargetName="BorderBrush" Storyboard.TargetProperty="Color" To="Red" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
            </Border>
        </ControlTemplate>
    </Window.Resources>

    <Grid>
        <Button Template="{StaticResource btnone}" Content="Button" Height="37" HorizontalAlignment="Left" Margin="34,26,0,0" Name="button1" VerticalAlignment="Top" Width="85"  />
    </Grid>
</Window>
2018-04-25_110647.png
参考资料

样式设置和模板化

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 225,641评论 6 525
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,754评论 3 408
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 173,075评论 0 370
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 61,369评论 1 303
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 70,386评论 6 402
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,800评论 1 317
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,122评论 3 431
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,130评论 0 281
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,689评论 1 327
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,693评论 3 348
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,804评论 1 356
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,399评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,128评论 3 341
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,528评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,705评论 1 278
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,409评论 3 383
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,878评论 2 368

推荐阅读更多精彩内容