【再学WPF】自定义样式

北慕城南 提交于 2019-11-27 15:30:00

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}"/>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!