Material 在 XAML 中设计覆盖样式不起作用
Material Design In XAML override style not working
我在使用 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 上创建了一个示例项目。如果有人能看一下,我将不胜感激。
问题在 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>
我在使用 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 上创建了一个示例项目。如果有人能看一下,我将不胜感激。
问题在 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>