WPF Plugin - Window Style not Working

Hello everybody,

I have a small problem with my wpf plugin. It has to do with the display of the wpf window when trying to use styles.

I have recreated the problem here, in case you want to look at it in depth.
The wpf window is opened with test.testCommand and it works fine until I try to apply a style to the wpf window (WpfApplication1.MainWindow.xaml):

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        Style="{StaticResource CustomWindow}">
</Window>

Style="{StaticResource CustomWindow}" naturally links to the App.xaml file but it crashes Rhino when executed this way. If Style="{StaticResource CustomWindow}" is omitted everything works again.

The App.xaml looks like this:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="CustomWindow" TargetType="{x:Type Window}">
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="BorderBrush" Value="Green"/>
            <Setter Property="BorderThickness" Value="5"/>
            <Setter Property="Foreground" Value="DarkRed"/>
        </Style>
    </Application.Resources>
</Application>

I couldn’t find anything about this online. Is this a known issue or am I forgetting something?
Thanks for your help!

Keep in mind that your plug-in is DLL, not an application-level object.

To share style resources in your DLL, you will first need to create a style dictionary. Then you will need to merge the dictionary with your window resources in you xaml file.

I don’t have an example that I can point you to, but you should be able to find plenty of resources on the web.

Does this help?

– Dale

Hello @dale,
thank you for the reply.

I am looking into this syntax, but I am not sure where to place the style dictionary:

<ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Resources;component/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>

I have two projects in my solution. Project1 is the Rhino plugin with a command which opens Project2 as shown below. Project2 is a wpf application. Is that maybe the problem or can it work like this?

using WpfApplication1;

namespace test
{
    [System.Runtime.InteropServices.Guid("efe72d01-5dfe-428e-afe9-fd82d5dbb2df")]
    public class testCommand : Command
    {
        // Removed code for clarity
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            System.Windows.Window my = new WpfApplication1.MainWindow();
            new System.Windows.Interop.WindowInteropHelper(my).Owner = Rhino.RhinoApp.MainWindowHandle();
            my.Show();
            return Result.Success;
        }
    }
}

This is the magic incantation for importing the styles

<Window.Resources>
   <ResourceDictionary x:Uid="ResourceDictionary_1">
     <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary x:Uid="ResourceDictionary_2" Source="..\..\Themes\Generic.xaml"/>
     </ResourceDictionary.MergedDictionaries>
     ….
 </Window.Resources>

Hey,

i stumbeld upon a similar error.

I tried to import a style into a WPF window. With the hint of dale…

Keep in mind that your plug-in is DLL, not an application-level object.

… i could fix the issue.

This import solved it for me:

 <Window.Resources>
     <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="pack://application:,,,/MyAssembly;component/<pathToFile>/style1.xaml"/>
             <ResourceDictionary Source="pack://application:,,,/MyAssembly;component/<pathToFile>/style2.xaml"/>
         </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
 </Window.Resources>
  • Exchanging “MyAssembly” to the Assembly name of the plugin, because the plugin’s assembly is not the entry assembly, and WPF’s default resource lookup failed for me
  • Keeping “component” in the import path
  • Setting according to my project folder
1 Like