【再學WPF】自定義樣式


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM