WPF样式

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:UC="clr-namespace:WpfApplication1"
        xmlns:UC1="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <!--
        Button控件样式
        -->
        <Style TargetType="Button">
            <Setter  Property="Background" Value="Pink"/>
            <Setter Property="FontSize" Value="22"/>
        </Style>
        <!--
        XAML的Class
        -->
        <Style x:Key="mystyle" TargetType="ContentControl">
            <Setter  Property="Background" Value="Peru"/>
            <Setter Property="FontSize" Value="30"/>
        </Style>

        <!--
        继承mystyle
        -->
        <Style x:Key="mystyle2" TargetType="ContentControl" BasedOn="{StaticResource ResourceKey=mystyle}">
            <!--覆盖基类样式-->
            <Setter  Property="Background" Value="Red" />
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button  Content="Button" Grid.Row="0"/>
        <Button Style="{StaticResource ResourceKey=mystyle}"
                Content="XAML的Class" Grid.Row="1"/>
        <Button Style="{StaticResource ResourceKey=mystyle2}"
                Content="样式的继承mystyle" Grid.Row="2"/>
        <Button  Content="Button嵌入样式" Grid.Row="3">
            <Button.Resources>
                <Style TargetType="Button" >
                    <Setter Property="Background" Value="Coral"></Setter>
                </Style>
            </Button.Resources>
        </Button>
    </Grid>
</Window>

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:UC="clr-namespace:WpfApplication1"
        xmlns:UC1="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="TriggersStyle" TargetType="Label">
            <Setter  Property="Background" Value="BurlyWood"/>
            <Style.Triggers>
                <!--Triggers单一条件触发事件 当IsMouseOver的时候,Button颜色变成粉色 -->
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter  Property="Background" Value="Pink"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="MultiTriggerStyle" TargetType="Button">
            <Setter  Property="Background" Value="BurlyWood"/>
            <Style.Triggers>
                <!--MultiTrigger多条件触发事件 当IsMouseOver后IsPressed时候,字体30号 -->
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsMouseOver" Value="True"></Condition>
                        <Condition Property="IsPressed" Value="True"></Condition>
                    </MultiTrigger.Conditions>
                    <Setter  Property="FontSize" Value="30"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="DataTriggerStyle" TargetType="Control">
            <Setter  Property="Background" Value="Brown"/>
            <Style.Triggers>
                <!-- 绑定当前的radio单选框,如果按钮选中,触发字体设置 -->
                <DataTrigger Binding="{Binding ElementName=radio, Path=IsChecked}" Value="True">
                    <Setter Property="FontSize" Value="30"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Style="{StaticResource ResourceKey=TriggersStyle}" Content="Triggers"></Label>
        <Button Style="{StaticResource ResourceKey=MultiTriggerStyle}" Content="MultiTrigger" Grid.Row="1"></Button>
        <RadioButton Style="{StaticResource ResourceKey=DataTriggerStyle}" Name="radio" Content="我要变成20号字" Grid.Row="2"></RadioButton>
    </Grid>
</Window>
时间: 2024-08-25 08:39:06

WPF样式的相关文章

wpf样式绑定 行为绑定 事件关联 路由事件实例

代码说明:我要实现一个这样的功能  有三个window窗口  每个窗体有一个label标签  当我修改三个label标签中任意一个字体颜色的时候  其他的label标签字体颜色也变化 首先三个窗体不用贴代码了  直接添加三个就行了 样式绑定: 先添加数据源  代码如下: (注:为了防止propertyName硬编码写死   可以使用CallerMemberName附加属性来获取默认的属性名称 或者使用表达式目录树Expression<Func<T>>的方式来获取) 1 public

WPF 样式(Style)初体验 (一) 作用域

刚刚接触WPF的开发,顿时对样式设计产生了兴趣.因为之前对CSS比较感兴趣,不难发现WPF的Style和CSS的模式很类似.下面我就根据自己初步的理解和CSS样式表对比做下总结,有理解不正确的地方还希望各位前辈指正. (1)全局样式控制 影响的是整个项目的样式,我们可以在App.xaml文件里定义全局的样式(这里就类似于我们在一个web的项目中添加一个全局的CSS文件,然后在每个页面引用CSS样式): <Application.Resources> <Style TargetType=&

WPF 样式设计之——radioButton

如何在WPF的cs文件中定义图片RadioButton呢?把图片加到了resource文件里面,希望在窗口中显示图片radiobutton,请问这样的radiobutton应该怎么定义呢?以下为处理的样式: XML code <RadioButton.Style> <Style TargetType="{x:Type RadioButton}"> <Style.Resources> <Style x:Key="CheckRadioFo

写自己的WPF样式 - 按钮

做一个后台管理小程序,据说WPF的界面比较"炫",于是选择使用WPF来开发.既然用了WPF当然需要做好看点了,于是稍微研究了下WPF的样式,废话不多说下面开始自定义一个按钮样式: (1)在App.xaml文件里自定义一个按钮样式 ,"MyWpfButton": <Application x:Class="WPFCustomerStyleStudy.App" xmlns="http://schemas.microsoft.com/w

WPF样式和资源2

<Window.Resources> <FontFamily x:key="ButtonFontFamily">Time New Roman</FontFamily> <sys:Double x:key="ButtonFontSize">18</s:Double> <FontWeight x:key="ButtonFontWeight">Bold</FontWeight

WPF样式引用

为了提高样式的重用性,统一配置管理.web将样式写成通用的css,然后在页面中调用css.同样,wpf同样有这样的机制.其中有一个全局控制的地方,那就是在app.xaml中引用样式. 方法如下: <Application x:Class="WpfStudy.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mic

WPF 样式和行为

样式是属性值的集合,能被应用到一个元素,类似CSS,每个控件最多只能有一个样式,通过控件的Stype属性应用样式,如下代码,其中BigFontButtonStyle是用于检索资源的关键字,也可以通过代码指定样式: <Button Padding="5" Margin="5" Name="cmd" Style="{StaticResource BigFontButtonStyle}"> </Button>

wpf 样式的调用

这个针对异地调用: 1.在主程序的项目中新建一个Skins的目录.然后再目录里新建一个BlackSkin.xaml的字典资源: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="M

WPF样式之画刷结合样式

第一种画刷,渐变画刷GradientBrush (拿线性渐变画刷LinearGradientBrush(其实它涵盖在GradientBrush画刷内.现在拿他来说事.),还有一个圆心渐变画刷RadialgradientBrush,两者用法相同): 我想象中的button的总体样式大体结构应该如下: <Style TargetType="Button"> <Setter Property="Template"> <Setter.Value