首页 文章

Material Design在XAML覆盖样式中不起作用

提问于
浏览
1

我在使用MaterialDesignInXamlToolkit覆盖非常简单的TextBox样式时遇到了困难 .

据我所知,我跟着override instructions一封信:

App.xaml中

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="Themes/MaterialDesignTheme.Overrides.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:materialDesign="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
        Title="MainWindow" Height="400" Width="300">
    <Grid>
        <TextBox VerticalAlignment="Center"
                 x:Name="NameTextBox"
                 materialDesign:HintAssist.Hint="Name">
        </TextBox>
    </Grid>
</Window>

MaterialDesignTheme.Overrides.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:materialDesign="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf">

    <Style x:Key="MaterialDesignTextBox" 
           BasedOn="{StaticResource MaterialDesignTextBox}"
           TargetType="{x:Type TextBox}">
        <Setter Property="FontSize" Value="200" />
    </Style>

</ResourceDictionary>

但是,除非我从覆盖文件中的样式中删除 x:Key ,否则文本框中的字体会保持非常无聊的12,而不是我之后的超级激动 200 .

我在Github here上创建了一个示例项目 . 如果有人可以看看我会非常感激 .

1 回答

  • 4

    问题出在MaterialDesignTheme.Overrides.xaml中 . 您指定要覆盖的特定样式,但不引用包含该样式的资源字典 . 合并它将解决问题 .

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.TextBox.xaml" />
        </ResourceDictionary.MergedDictionaries>
    
        <Style BasedOn="{StaticResource MaterialDesignTextBox}"
               TargetType="{x:Type TextBox}">
            <Setter Property="FontSize" Value="200" />
        </Style>
    </ResourceDictionary>
    

相关问题