【再學WPF】自定義樣式

一、添加「資源字典」;app

工程名稱:WpfApp1
新建Styles文件夾;
建立「Dictionary1.xaml」的文件;

 

二、編輯樣式;spa

    <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>

 

三、在App.xaml中引用樣式code

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

四、元素上添加樣式component

        <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}"/>
相關文章
相關標籤/搜索