我在使用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 I'之后。
我在Github here上创建了一个示例项目。如果有人能看一眼,我会非常感激。
答案 0 :(得分: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>