6
votes

I've been working on a commandline application, and have recently decided to add a wpf window to the application. I added this as a UserControl, however I noticed I can't call this class using ShowDialog() from my main code;

I've tried changing the Base class from a UserControl to Window, however an error occurs;

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

Error 1 Partial declarations of 'ExcelExample.UserControl1' must not specify different base classesExcelExample

I've added all the references found in my other WPF application to no avail. Help!

1
Not sure about the ShowDialog() issue, but the reason you're getting the error when you change to a different subclass is that there is a definition of UserControl1 elsewhere (which is what "partial" means), probably in the file where InitializeComponent() lives and related to your user interface.Michael Todd
Stumped, done a global search for UserControl1, and I can only find references in the XAML file. I can see the document is a System.Window.Controls.UserControl Can I convert the document to a WPF doc, and what project setting do I need to change to enable the adding of a basic WPF form?wonea

1 Answers

11
votes

In order to change the base class it is not sufficient to change it in code only. You must also change the root tag and any nested elements in accompanying XAML file. For example, you have something like:

<UserControl x:Class="Your.Namespace.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <UserControl.Resources>
     </UserControl.Resources>
</UserControl>

You must change it to something like:

<Window x:Class="Your.Namespace.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <Window.Resources>
     </Window.Resources>
</Window>