0

I created user controller named MainControl.xaml. Inside my MainWindow.xaml (which is empty, blank) I wan to to insert this MainControl control.

So inside MainWindow loaded event I put

private void Window_Loaded(object sender, RoutedEventArgs e) { var bc = new Controls.BooksControl(); bc.Visibility = System.Windows.Visibility.Visible; } 

but nothing happens, obviously I'm missing something

3
  • Can you also post some XAML of your MainWindow? Commented Dec 8, 2013 at 17:08
  • main window is empty so it really has nothing inside xaml worth to post, window init. code and empty grid tags. Commented Dec 8, 2013 at 17:13
  • In that case you can set the Content of the Grid to bc. Although you might want to add your control directly in XAML. Commented Dec 8, 2013 at 17:14

3 Answers 3

2

You should add your control to the window (set this new control as content of the window):

private void Window_Loaded(object sender, RoutedEventArgs e) { var bc = new Controls.BooksControl(); bc.Visibility = System.Windows.Visibility.Visible; this.Content = bc; } 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to add it to an actual container, in order to show it. For instance a Grid or an StackPanel. If you add a custom clr-namespace you can also add your Control directly from inside your XAML.

Comments

1

I'm going to assume the MainControl that you've mentioned is actually the BooksControl that is instantiated in your code that you've exposed.

Yes, you've created a new instance in your code-behind but from what I can see you haven't done anything to actually add it to the layout (particularly given that you've mentioned that your MainWindow.xaml is empty).

Now, I'm also going to assume that when you say "but nothing happens" that you mean that your BooksControl isn't showing in your MainWindow - this is because, as described, you haven't added it to the layout.

The two main ways to do this are in the XAML or in the code behind:

XAML:

<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xlmns:x="http://schemas.microsoft.com/winfx/2006/xaml" controls="clr-namespace:Controls;assembly=Controls"> <controls:BooksControl/> </Window> 

Code Behind

private void Window_Loaded(object sender, RoutedEventArgs e) { var bc = new Controls.BooksControl(); // set the content of the Window to be the BooksControl // assuming the BooksControl has default Visibility of Visible this.Content = bc; } 

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.