WPF 美化界面,樣式的使用


           在我看來,學習WPF,最重要的還是學習它的布局,樣式的使用,類似於web頁面布局的優點是winForm所不能及的,可以通過它靈活的布局,樣式的添加,從而制作出很多很炫的界面,下面就簡單的總結下關於WPF中樣式的幾種用法:

         我們以按鈕Button為例,比如改變它的背景顏色或者添加圖片背景,在這里需要說明的是,不是每一種樣式都能實現同樣的效果

         方法一:直接在button里使用Background賦值即可,這個是最簡單的,但是有些樣式會實現不了,一些簡單的還是可以的

 <Button Content="Button" Height="23" Background="Yellow" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

        方法二、通過Button.Background為其添加圖片背景

   <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,47,0,0" Name="button2" VerticalAlignment="Top" Width="75">
            <Button.Background>
                <ImageBrush ImageSource="/Images/按鈕背景.png" />
            </Button.Background>
        </Button>

       在這里說明一下,圖片的路徑也可以改成 <ImageBrush ImageSource="/項目程序集名;component/Images/按鈕背景.png" />
     

       下面的幾種方法都是通過資源來完成的(關於資源,在后面會講到)

        方法三、內部定義資源如:Button.Resources    

    <Button Content="Button" Height="41" HorizontalAlignment="Left" Margin="10,126,0,0" Name="button4" VerticalAlignment="Top" Width="75">
            <Button.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="#FF1F3B53"/>
                </Style>
            </Button.Resources>
        </Button>

     方法四、在窗體中定義資源,然后再控件中調用

   首先,定義一個資源

    <Window.Resources>
        <Style x:Key="buttonStyle" TargetType="Button">
            <Setter Property="Foreground" Value="#999999"/>
        </Style>
     </Window.Resources>

然后就可以再控件中調用了
 

        <Button Content="Button" Height="23" Style="{StaticResource buttonStyle}"  HorizontalAlignment="Left" Margin="10,204,0,0" Name="button5" VerticalAlignment="Top" Width="75" />

    方法五、類似web中在外部定義樣式,然后再窗體中調用

     處理方式一、

     首先,定義個外部樣式ButtomStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Height" Value="500"></Setter>
        <Setter Property="Foreground" Value="#aaaaaa"/>
        <Setter Property="Background" Value="#FF1F3B53"/>
    </Style>
</ResourceDictionary>

     然后再窗體中引入這個樣式

 <Window.Resources>
          <ResourceDictionary x:Key="butStyle">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="style/ButtomStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

注意:引入樣式的時候需要加一個key,這樣下面的才可以調用
    然后就可以再控件中調用了

    

  <Button Content="Button" Height="23" Resources="{StaticResource butStyle}" HorizontalAlignment="Left" Margin="152,12,0,0" Name="button6" VerticalAlignment="Top" Width="75" />

方式處理二、在App中加載樣式
      定義外部的樣式為:

     

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button" x:Key="bstyle">
        <Setter Property="Height" Value="500"></Setter>
        <Setter Property="Foreground" Value="#aaaaaa"/>
        <Setter Property="Background" Value="#dddddd"/>
    </Style>
</ResourceDictionary>

然后再app中加載

<Application x:Class="WpfApplication2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="樣式的用法/ButtonStyleDemo.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="樣式的用法\Style\buttonStyleApp.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

這樣直接調用樣式的key即可

        <Button Content="Button" Height="23" Style="{StaticResource bstyle}" HorizontalAlignment="Left" Margin="152,58,0,0" Name="button7" VerticalAlignment="Top" Width="75" />

如果先弄一個全局的樣式,那么不需要定義key即可,也不用再控件中調用,這樣就行了

 

上面是就button為例子,簡單介紹了一下wpf中布局中所用到的資源,樣式等,具體的到時候遇到了問題,在具體解決一下!

 

 

 

 

    

  

 

     

  

        

       


免責聲明!

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



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