WPF style

Method 1, Window.Resources
<Window.Resources>
    <Style TargetType="Control Type" x:Key="Style Name">
        <Setter Property="Property Name" Value="Property Value"/>
    </Style>      
For example:
    <Style TargetType="Button">
        <Setter Property="Background" Value="Red" />
    </Style>
</Window.Resources>

call style
<Button Style="{StaticResource style name}">

style event
<EventSetter Event="Control Type.Event Type" Handler="Event Name" />
For example:
<EventSetter Event="TextBlock.MouseEnter" Handler="tb_MouseEnter" />

Style inheritance
<Style x:Key="Style name" BasedOn="{StaticResource base class style name}">

Style Triggers
<Style TargetType="Control Type">
    <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>     <ResourceDictionary><Application.Resources>call external resources
</Application.Resources>




        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/assembly;component/path.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

3. Background Call resource
ResourceDictionary rd= new ResourceDictionary();
rd.Source = new Uri("assembly;component/path.xaml", UriKind.Relative);
call style
SolidColorBrush blueBrush =(SolidColorBrush)rd["BlueBrush"];


<! --Make it not refer to a pre-defined style-->
<Control Style="{x:Null}">
eg
<Button Style="{x:Null}">

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652857&siteId=291194637