WPF style 样式的使用

一、在标签里内联样式

 <Button x:Name="btn_danru" Click="btn_danru_Click" Width="200" >

            <Button.Style>

                <Style TargetType="{x:Type Button}">

                    <Setter Property="Template">

                        <Setter.Value>

                            <ControlTemplate TargetType="{x:Type Button}">

                                <Image Name="btnbg" Source="Images/btn_normal.png" />

                                <ControlTemplate.Triggers>

                                    <Trigger Property="IsMouseOver" Value="True">

                                        <Setter Property="Source"  Value="/Images/btn_over.png" TargetName="btnbg" />

                                    </Trigger>

                                    <Trigger Property="IsPressed" Value="True">

                                        <Setter Property="Source" Value="/Images/btn_down.png" TargetName="btnbg" />

                                    </Trigger>

                                    <Trigger Property="IsEnabled" Value="false">

                                        <Setter Property="Source" Value="/Images/btn_unenable.png" TargetName="btnbg" />

                                    </Trigger>

                                </ControlTemplate.Triggers>

                            </ControlTemplate>

                        </Setter.Value>

                    </Setter>

                </Style>

            </Button.Style>

     </Button>

二、内嵌式

在页面<Window.Resources>节点下添加样式,然后在需要的控件上设置Style属

 <Window.Resources>

       <Style x:Key="ButtonStyle34" TargetType="{x:Type Button}">

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type Button}">

                    <StackPanel Orientation="Horizontal" Background="Transparent" Margin="0,0,0,0" >

                        <Image Name="ImgBtnBg3" Margin="0,0,0,2" Source="/Resources/Res/Delete_I.png" Stretch="Fill" Width="54"/>

                    </StackPanel>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter Property="Source" Value="/Resources/Res/Geng_II.png" TargetName="ImgBtnBg3"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter Property="Source" Value="/Resources/Res/Geng_III.png" TargetName="ImgBtnBg3"/>

                        </Trigger>

                        <Trigger Property="IsEnabled" Value="False">

                            <Setter Property="Source" Value="/Resources/Res/Delete_I.png" TargetName="ImgBtnBg3"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

 

    </Style>

 </Window.Resources>

在页面:

<Button Style="{DynamicResource ButtonStyle34}" >

三、外联样式

1、新建一个.xaml资源文件,如/Theme/Style.xaml
2在Style.xaml文件里编写样式代码
Style.xaml: 
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<Style x:Key="myBtnStyle" TargetType="Button">
<Setter Property="Height" Value="72" />
<Setter Property="Width" Value="150" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Blue" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
</ResourceDictionary>

3,在App.xaml文件的<Application.Resources>
或者普通页面的<Window.Resources>
或者用户控件的 <UserControl.Resources> 节点下
添加相应的ResourceDictionary,配置引用Style.xaml:
app.xaml:
<Application.Resources> 

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/应用名称;component/Theme/Style.xaml"/>
<!--<ResourceDictionary Source="Resources/BtnStyle2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

</Application.Resources>
或者MainWindow.xaml:
<Window.Resources>

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/BtnStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

</Window.Resources>
<ResourceDictionary.MergedDictionaries>节点下可以添加多个资源文件
这种方式相比前面两种使得样式和结构又更进一步分离了。
在App.xaml引用,是全局的,可以使得一个样式可以在整个应用程序中能够复用。在普通页面中引用只能在当前页面上得到复用。

4, 设置Botton控件的Style属性为"{StaticResource myBtnStyle}" 和上面的一样。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值