-2

I'm having an issue where the styles don't seem to apply in the WPF designer, so it only shows the default ones but works correctly at runtime.

I'm using MaterialDesignInXaml which worked fine before.

I've tried reinstalling Rider several times, followed the quick start guide, and copied everything it had in new projects. I've also tried MahApps which gave the same issue. and it does seem like it's a Rider issue only as it works fine in visual studio 22.

Here's an image that shows the difference between the WPF preview (Above) and the app at runtime

Above is the WPF Designer and below is the app at runtime

As for the code:

  • MainWindow.xaml
<Button Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"
                    Content="Button 1" FontSize="18"
                    Style="{DynamicResource MaterialDesignFlatButton}" />
<Button Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Button 2"
                    FontSize="15"
                    Style="{DynamicResource MaterialDesignFlatButton}" />
<Button Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Button 3"
                    FontSize="15"
                    Style="{DynamicResource MaterialDesignFlatButton}" />
  • App.xaml
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <materialDesign:BundledTheme BaseTheme="Dark" PrimaryColor="Teal" SecondaryColor="Lime" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Silviu
  • 70
  • 1
  • 1
  • 7

2 Answers2

2

You can try this in your xaml:

    <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <wpfcommon:DesignTimeResourceDictionary DesignTimeSource="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

DesignTimeResourceDictionary.cs

public class DesignTimeResourceDictionary : ResourceDictionary
{
    /// <summary>
    /// Local field storing info about designtime source.
    /// </summary>
    private string _designTimeSource;

    /// <summary>
    /// Gets or sets the design time source.
    /// </summary>
    /// <value>
    /// The design time source.
    /// </value>
    public string DesignTimeSource
    {
        get
        {
            return _designTimeSource;
        }

        set
        {
            _designTimeSource = value;
            if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
            {
                base.Source = new Uri(_designTimeSource);
            }
        }
    }

    /// <summary>
    /// Gets or sets the uniform resource identifier (URI) to load resources from.
    /// </summary>
    /// <returns>The source location of an external resource dictionary. </returns>
    public new Uri Source
    {
        get
        {
            return base.Source;
        }

        set
        {
            throw new Exception("Use DesignTimeSource instead set Source!");
        }
    }
}
bazsisz
  • 394
  • 2
  • 11
0

Downgrading Rider to 2021.3.4 fixed this.

Silviu
  • 70
  • 1
  • 1
  • 7