Style "Cannot find resource 'MetroGroupBox'" in MahApps at runtime

27 Views Asked by At

I am using MahApps Metro for a project and I'm attempting to modify a particular style. In the Resource Dictionary, there is no error for the following code:

/Resources/Resources.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">

    <LinearGradientBrush x:Key="GroupBoxBrush" EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
        <GradientStop Color="Black" Offset="0"/>
        <GradientStop Color="#FF58DCF9" Offset="1"/>
        <GradientStop Color="#FF173B43" Offset="0.01"/>
        <GradientStop Color="#FF317C8C" Offset="0.012"/>
        <GradientStop Color="#FF3FA0B5"/>
    </LinearGradientBrush>

    <Style x:Key="GroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource MetroGroupBox}">
        <Setter Property="Header" Value="{Binding}" />
        <Setter Property="FontFamily" Value="Calibri" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="TextOptions.TextFormattingMode" Value="Ideal" />
        <Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="14"/>
        <Setter Property="Background" Value="{StaticResource GroupBoxBrush}" />
        <Setter Property="VerticalAlignment" Value="Top"/>
    </Style>

App.xaml:

<Application.Resources>
    <ResourceDictionary Source="/Resources/Resources.xaml">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    
    </ResourceDictionary>
</Application.Resources>

Then I use the style in a GroupBox:

<GroupBox Header="Customers" Style="{StaticResource GroupBoxStyle}" Width="150" Height="210">

However, at runtime, it throws the error when trying to build the control with that GroupBox:

System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '35' and line position '34'.'
Inner Exception:
Exception: Cannot find resource named 'MetroGroupBox'. Resource names are case sensitive.

I'm using Visual Studio 2022, MahApps.Metro version 1.1.2. I have tried putting this in the Style instead: BasedOn="{StaticResource Controls.MetroGroupBox}" but that didn't work.

EDIT

I have found that it does work if I put the style in the control.xaml. I have a lot of controls that use this style, though, so although it is better than redefining the GroupBox every time, it is still a lot of extra duplication of the style.

0

There are 0 best solutions below