Opening a Wpf form from Rhino Common C# Plugin

HI ,

How can we open a wpf form as popupform in Rhinocommon plugin c#.

We are using the windows usercontrol for the main dock panel for the plugin
when users click on the button in the user dock panel, object of winform is created and show method will display the form.
We are opening the form like below this is for displaying some details

PopupForm popUp = new PopupForm ();
popUp.Show();

This works fine, but we need to put some ui rich elements.
if we created instance of the wpfForm (popup.xaml) , there are no methods like show();

I can see examples for wpf as dock panels here https://github.com/mcneel/rhino-developer-samples/tree/6/rhinocommon/cs/SampleCsWpf

but what I need to popup a form like a messagebox or dialog box.

can anybody give some insights on this.
thanks,

Hi, the best approach is to simply create your user controls within a external library using Visual studio and simply invoke them from your custom command. This will also allow you to check it from a separate program.
Make sure to invoke the window from the GUI Thread. This might be relevant if your are within an event callback. Yes basically what you showed is the correct way. You could also choose .ShowDialog() if you want it to have a dialog-like behaviour. Make also sure your main control is a window and not a uc.

Hi @nevin.mathew,

https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/dialog-boxes-overview

– Dale

Thanks alot Tom and Dale for your quick response.

The issue I was highlighting is I was not getting the Show or Show Dialog for the wpf control.

The reason is Rhino C# Project template we dont have option to add wpf pages or forms ,we can only add wpf usercontrol.

is there any reason why the wpf page or forms not showing in the add new items in RhinoCommon plugin project in Visual Studio? I am using 2019 Community Edition

But we can add windows forms.
IF we add a wpf user control, that doesnt have show or showdialog method, might be able to extend the control ,am not sure.

Thanks Dale for the link,

IF we add a new wpf user control we have the xaml like below

UI XAML will be like below

<UserControl x:Class="ImageObjectsPanel.PopUpMessage "
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ImageObjectsPanel"
          </UserControl>

code behind like below

public partial class PopUpMessage : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }

I udpated the above Xaml with window tag and in code behind inheriting from window instead of usercontrol , then I was able to access the show or showdialog

<Window x:Class="ImageObjectsPanel.PopUpMessage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ImageObjectsPanel"
             mc:Ignorable="d" 
             Background="Gray"
            ShowInTaskbar="False"
            WindowStartupLocation="CenterOwner"
             d:DesignHeight="200" d:DesignWidth="200"
             Height="200" Width="300"
        >
</Window>

public partial class PopUpMessage : Window
    {
        public PopUpMessage()
        {
            InitializeComponent();
        }
 
    }

After this changes I was able to display a dialog box using the show dialog, I am not sure this is stable or it can break the code in the future or it may behave differently once I deploy to other machines , I haven’t used the this for real scenarios , need to test it .

Uploaded the plugin here
[https://github.com/Nevviin/RhinoCommon/tree/master/WpfDialog]

Advanced Easter wishes

Hi @nevin.mathew,

Have you made any progress on this? I have not reviewed your code yet.

– Dale

Thanks Dale for for your reply.
I was able to test it from a sample Dock panel application as mentioned above ,
Its just a plain usercontrol dock with a button, on click of the button it will show a wpf dialog box with progress bar “marquee”.
the dialog box is showing up perfectly.

I am not sure why we don’t have a Wpf page option for add >> new item in Rhino Plugin project, but a the same time we have the win form item to add.

but I didn’t get a chance to incorporate in our core plugin, since we are working on other changes and bug fixes for the plugin

Hi @nevin.mathew,

When you right-click on your project and Solution Explorer and click Add > New Item..., your not seeing this?

– Dale

thanks for your reply
May be I haven’t used much wpf and it was not very straight forward for me to change the user control inheritance
from : public partial class PopUpMessage : UserControl
To : public partial class PopUpMessage : Window to get the window properties.

In wpf solution we have option for adding new wpf page, page has the method to show().

But RhinoCommon plugin project doesn’t have that, it has only the wpf user control. need to update the inheriting class as above .
Rhinocommon plugin solution


wpf solution

thanks,
Nevin