Placing a WPF User Control Inside a StackPanel

I am trying to create my own WPF control and put it inside a StackPanel in my XAML file. I initially made a custom UserControl and received the following error message:

 A value of type 'CustomControl' cannot be added to a collection or dictionary of type 'UIElementCollection'. 

I tried changing my custom control from UserControl to UIElement , but I still get this message. How to create your own control that can be placed inside a StackPanel ?

+4
source share
3 answers

How do you create a customcontrol? Make sure it inherits from UserControl.

I just created a new project called "TestProj" - I right-clicked in the Add => UserControl solution explorer and named it CustomControl.

I managed to insert it through the following code:

 <Window x:Class="TestProj.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProj" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel> <local:CustomControl/> </StackPanel> </Grid> 

+3
source

Restart Visual Studio. The XAML designer is a famous monstrous beast.

+6
source

This can also happen if you subclass a different management class, but your .xaml.cs file still subclasses UserControl.

0
source

All Articles