Skip to content

C# WPF: How to get UserControl from MainWindow?

Giovanbattista Barra

Asked Dec 7, 2017, 12:09 AM EST2 answers
Share
LinkedInX
Hallo,
 
I made a small App in WPF. This is a drawing tool.
My chef decided to integrate my app in the existing software of our company
and I'm now a little bit stuck because I haven't used yet any UserControl beucause it was supposed to be an "independant" App.
Now the question:
 
Can I change the existing MainWindow.xaml in UserControl?
 What I mean is:
 
<Window
       DataContext="MainViewModel" x:Class="Skizzen_Editor.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:Skizzen_Editor"
      mc:Ignorable="d"
      Name="Win_SkizzenEditor" Background="Transparent"
 
</Window> 
 
here I've just changed <Window> in <UserControl>
but something is going wrong. It seems It doenst work.
Can you please suggest me a solution?
Or is it better to make a new UserControl and out the existing MainWindow.xaml inside of it? 
 
Thanks in advance 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Rafnas T P
    0
    hi,
    you can change window to user control,
    just by changing <Window> to <UserControl>
    and </Window>  to </UserControl>
     
    if any error comes just comment those lines and try. error comes if anything depending on user control, remain all same 
    0
  • 0
    Thanks for the reply.
    Anyway I have another problem.
    After I changed the tags from <Window> </Window>
    into <UserControl> </UserControl>
    I have two error messages by:
    1) the closing tag </UserControl> and it says:
    "the token UserControl is unexpected" 
     
    2)  <UserControl.Resources>  // only here it says: an Attribute name is missing
              <local:EnumMatchToBooleanConverter x:Key="enumConverter" />
        </UserControl.Resources>  // here no error message
    From many internet sources it seems to be right with <UserControl.Resources>
     
    ANY Help or Ideas? 
    0