I'm trying to create a custom WPF control and place it inside of a StackPanel
in my XAML file. I originally made a custom UserControl
and got 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 a UserControl
to a UIElement
, but I still get this message. How do I make a custom control that I can place inside of the StackPanel
?
Restart Visual Studio. The XAML designer is a notoriously sensitive beast.
How are you creating the CustomControl? Make sure it is inheriting from UserControl.
I just created a new project called "TestProj" - right clicked in the solution explorer Add=>UserControl and named it CustomControl.
I was able to insert it via 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With