WPF样式

方式一、Window.Resources
<Window.Resources>
    <Style TargetType="控件类型" x:Key="样式名">
        <Setter Property="属性名"  Value="属性值"/>
    </Style>      
例如:
    <Style TargetType="Button">
        <Setter Property="Background" Value="Red" />
    </Style>
</Window.Resources>

调用样式
<Button Style="{StaticResource 样式名}">

样式事件
<EventSetter Event="控件类型.事件类型" Handler="事件名" />
例如:
<EventSetter Event="TextBlock.MouseEnter" Handler="tb_MouseEnter" />

样式继承
<Style x:Key="样式名" BasedOn="{StaticResource 基类样式名}">

样式触发器
<Style TargetType="控件类型">
    <Style.Triggers>
        <Trigger Property="Control.触发类型" Value="True">
            <Setter Property="Control.属性名" Value="属性值" />
        </Trigger>
    </Style.Triggers>
</Style>
例如
<Style TargetType="Button">
    <Trigger Property="Control.IsFocused" Value="True">
        <Setter Property="Control.Foreground" Value="DarkRed" />
    </Trigger>
</Style>

二、Application.Resources
<Application.Resources>
    <Style TargetType="控件类型" x:Key="样式名">
        <Setter Property="属性名"  Value="属性值"/>
    </Style>
</Application.Resources>

调用外部资源
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/程序集;component/路径.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

三、后台调用资源
ResourceDictionary rd= new ResourceDictionary();
rd.Source = new Uri("程序集;component/路径.xaml", UriKind.Relative);
调用样式
SolidColorBrush blueBrush =(SolidColorBrush)rd["BlueBrush"];


<!--使其不引用事先定义的样式-->
<控件 Style="{x:Null}">
例如
<Button Style="{x:Null}">

猜你喜欢

转载自www.cnblogs.com/sntetwt/p/8888688.html