0

I created a user control in a project that consists of just a MainWindow.xaml and the code behind. I added the .dll to the toolbox of VS, and dropped it onto a window in a new project. This created the following:

<Window 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:WpfApplication5" xmlns:ThinkGeoClean="clr-namespace:ThinkGeoClean;assembly=ThinkGeoClean" x:Class="WpfApplication5.MainWindow" mc:Ignorable="d" Title="MainWindow" > <Grid> <ThinkGeoClean:ListBoxCustom x:Name="listBoxCustom" /> </Grid> </Window> 

The ThinkGeoClean is the name of the .dll I added which is the usercontrol. ListBoxCustom is just a public class in the control, but is NOT what I want to show. I want to show the main window of the usercontrol (not a window), but it doesn't show as an option after typing <ThinkGeoClean:. The only thing that shows up is ListBoxCustom. If I go ahead and type <ThinkGeoClean.MainWindow>, it gives a XamlParseException error on that line.

Now, if I go into the code-behind and do:

ThinkGeoClean.MainWindow newWin = new ThinkGeoClean.MainWindow(); newWin.Show(); 

It will pop up the usercontrol in a new window and it works fine.

Here's the beginning of usercontrol code behind:

namespace ThinkGeoClean { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { 

And the xaml for it is just a single grid containing some buttons and a map control.

Edit: In addition to the answer below, my user control was originally just a normal WPF project. I thought changing the output type to a class library would change it into a user control automatically, but I actually had to go into the xaml and change the the into .

5
  • 2
    Xaml can't show windows, if it could you could popup a message box from xaml. This is due to the visual tree not accepting Window as a child. It can only be a parent. Commented Jan 8, 2018 at 16:10
  • have u add the reference correctly ?! does it copy to your build location ?! Commented Jan 8, 2018 at 16:10
  • Shoot. Alright I'm going to try changing the control into an actual <UserControl/> instead of <Window/>. I thought I could just change the project output to "Class Library" and it would work, guess not though. Thanks! Commented Jan 8, 2018 at 16:13
  • 2
    Are you trying to make ThinkGeoClean.MainWindow the root element in your Xaml (e.g., replacing Window), or are you trying to make it a descendant of the Window? The former should be legal, but the latter is not. Commented Jan 8, 2018 at 16:14
  • I was trying to make it the descendant. Thank you. I have to go back to the control and change the window into usercontrol. Commented Jan 8, 2018 at 16:19

1 Answer 1

2

This XAML:

<Grid> <ThinkGeoClean:ListBoxCustom x:Name="listBoxCustom" /> </Grid> 

...is not equivalent to creating an instance of a window and call the Show() method on it programmatically.

Instead the XAML processor will try to add the window to the Children collection of the Grid and this is not possible since a window cannot be child of another control. That's why you get an exception.

Also, a UserControl must be hosted in a window or page. It is not a top-level control that you can display without any parent host.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.