如何将样式放在单独的.xaml文件中

时间:2015-06-04 20:50:10

标签: c# .net wpf xaml

我有一个应用程序,其中包含大量样式,这些样式当前在应用程序的每个窗口的.xaml中都是重复的。我希望能够引用一个名为UiStyles.xaml的文件,其中包含应用程序的所有样式。

在这里和谷歌阅读了大量已回答的问题后,我试过这个:

ButtonStyle.xaml:     

    <Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="FontSize" Value="48"/>
    </Style>
</ResourceDictionary>

UiStyles.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ButtonStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="Control" /> <!-- Added this based on other user's suggestions to account for .net 4 bug -->
</ResourceDictionary>

MainWindow.xaml:

<Window x:Class="TestingGround.UI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary Source="Resources/UIStyles.xaml"/>
    </Window.Resources>
    <Grid>
        <Button Click="ButtonBase_OnClick" Content="Test Text"/>
    </Grid>
</Window>

但是我的按钮样式没有应用!我做错了什么?

0 个答案:

没有答案