WPF 如何引入外部樣式

來源:互聯網
上載者:User

標籤:建立   blog   log   檔案   target   相對   gre   引用   方法   

   當我們給一些控制項設定相同的屬性的時候,這時候,我們可以把這些屬性寫到一個Style裡面。

   而其他頁面也有類似的控制項也需要使用這個Style,這時候就需要把這個Style放在一個共通的檔案裡,然後引入這個Style檔案即可。有點像html引入CSS檔案一樣。

   首先建立一個資源字典檔案,方法如下:

然後在裡面添加我們的樣式,如簡單寫一個Button的樣式:

 <Style x:Key="BtnStyle" TargetType="Button">        <Setter Property="Height" Value="72" />        <Setter Property="Width" Value="150" />        <Setter Property="Foreground" Value="White" />        <Setter Property="Background" Value="Green" />    </Style>

然後在我們的表單裡引用:

 <Window.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="Style/test.xaml"/>            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Window.Resources>

這裡的Soure裡寫的是相對路徑,因為我的test.xmal是放在Style檔案夾裡的,所以前面需要加Style。

使用的方法,如普通的方法是一樣的。

 <Button x:Name="button" Grid.Column="1"   Grid.Row="1" Style="{DynamicResource BtnStyle}" />

效果如下:

 

WPF 如何引入外部樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.