Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing Custom WPF Control inside of a StackPanel

Tags:

wpf

xaml

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?

like image 881
Jonathan Sternberg Avatar asked Jan 19 '23 11:01

Jonathan Sternberg


2 Answers

Restart Visual Studio. The XAML designer is a notoriously sensitive beast.

like image 162
Luke Puplett Avatar answered Jan 21 '23 12:01

Luke Puplett


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>

like image 34
codechinchilla Avatar answered Jan 21 '23 11:01

codechinchilla