Material Design In XAML override style not working

MarkNS picture MarkNS · Nov 26, 2017 · Viewed 7.3k times · Source

I'm having difficulties overriding a very simple TextBox style using the MaterialDesignInXamlToolkit.

As far as I can see I've followed the override instructions to the letter:

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>

However, unless I remove the x:Key from the style in the overrides file, the font in the text box stays at a very boring 12, rather than the super exciting 200 I'm after.

I've created an example project on Github here. If anyone could take a look I'd be very grateful.

Answer

Kevin B picture Kevin B · Nov 26, 2017

The issue is in MaterialDesignTheme.Overrides.xaml. You are specifying a specific style to override but not referencing the resource dictionary that contains that style. Merging it in will fix the problem.

<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>