Warrior Sun

内蒙古强网电子科技有限公司

导航

【再学WPF】自定义样式

Posted on 2019-08-17 14:09  Warrior Sun  阅读(926)  评论(0编辑  收藏  举报

1、添加“资源字典”;

工程名称:WpfApp1
新建Styles文件夹;
创建“Dictionary1.xaml”的文件;

 

2、编辑样式;

    <SolidColorBrush x:Key="MainColor">#FF000000</SolidColorBrush>
    <SolidColorBrush x:Key="MainColor1">#FFFFFFFF</SolidColorBrush>

    <Style  TargetType="Button">
        <Setter Property="Background" Value="Blue"/>
    </Style>

    <Style TargetType="TextBox">
        <Setter Property="FontFamily" Value="微软雅黑"/>
        <Setter Property="FontSize" Value="23"/>
    </Style>

 

3、在App.xaml中引用样式

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfApp1;component/Styles/Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

4、元素上添加样式

        <Button Content="主题颜色" Width="100" Height="40" Margin="545,82,155,328"/>
        <Button Content="主题颜色" Width="100" Height="40" Margin="545,127,155,283"/>

        <Button Content="外部样式" Width="100" Height="40" Margin="144,270,556,140" Background="{StaticResource MainColor}" Foreground="{StaticResource MainColor1}"/>