Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 132323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:14:40+00:00 2026-05-11T06:14:40+00:00

I have had major problems with getting UserControls to work in XAML – I

  • 0

I have had major problems with getting UserControls to work in XAML – I have spent hours trying to figure out all the problems but have got nowhere and cannot find where I am going wrong.
The main problem I am having is when I create a UserControl for example a simple one which shows an object different colours – I have successfully created a property for this and can assign the colours to this UserControl at Design Time and it works – shows Green, Red etc.
However when I give this UserControl a name so I can assign this Property at Runtime I get an error ‘Could not create instance of type ‘MyUserControl” if I remove the name the user control works – I can add as many as I want at Design Time and they all work but as soon as I assign a Name or x:Name it breaks and I cannot figure out why.
It is possible to create a Label for example and it has a name I can refer to it in code – why not my own control – no matter how simple.

My main problems are:

  1. Why does giving my UserControl a Name or x:name stop it from working?
  2. How to use multiple UserControls of the same type on a Window?
  3. How to access the Canvas, Label etc of a UserControl from inside or outside the UserControl?
  4. How to instance a UserControl at runtime or at design time in code or XAML?

I don’t get why these should be so difficult – I cannot figure out this issue so please if anyone can help then thanks!


Here is the XAML and Code for my UserControl

<UserControl x:Class='Device.Draco'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     Width='20' Height='36' x:Name='Icon'>     <Canvas Width='20' Height='36' HorizontalAlignment='Left' VerticalAlignment='Top'>         <Rectangle Height='36' Width='20' Fill='{Binding ElementName=Icon, Path=ZuneColour}' Canvas.Left='0' Canvas.Top='0' RadiusX='1' RadiusY='1'>             <Rectangle.BitmapEffect><OuterGlowBitmapEffect GlowColor='Black' GlowSize='2' /></Rectangle.BitmapEffect>         </Rectangle>         <Rectangle Canvas.Left='1' Canvas.Top='1' Height='24' Stroke='#191616' Width='18'>             <Rectangle.Fill>                 <LinearGradientBrush>                     <GradientStop Offset='1' Color='#231F20'/>                     <GradientStop Offset='0' Color='#524F4F'/>                     <LinearGradientBrush.Transform>                         <RotateTransform Angle='68' CenterX='0.5' CenterY='0.5'/>                     </LinearGradientBrush.Transform>                 </LinearGradientBrush>             </Rectangle.Fill>         </Rectangle>         <Rectangle Canvas.Left='5.5' Canvas.Top='25' Height='9' Width='9' RadiusX='3' RadiusY='3'>             <Rectangle.Fill>                 <LinearGradientBrush>                     <GradientStop Offset='0' Color='#66000000'/>                     <GradientStop Offset='1' Color='#22000000'/>                 </LinearGradientBrush>             </Rectangle.Fill>             <Rectangle.Stroke>                 <LinearGradientBrush>                     <GradientStop Offset='0' Color='#66FFFFFF'/>                     <GradientStop Offset='1' Color='#22FFFFFF'/>                 </LinearGradientBrush>             </Rectangle.Stroke>         </Rectangle>     </Canvas> 

This is the code for the UserControl – have added all the styles and fills as simple XAML to eliminate this as the cause – the Code Behind is below:

Namespace Device     Partial Public Class Draco         Inherits System.Windows.Controls.UserControl         Public Shared ZuneColorProperty As DependencyProperty = _         DependencyProperty.Register('ZuneColour', GetType(Brush), GetType(Device.Draco))         Public Property ZuneColour() As Brush             Get                 Return GetValue(ZuneColorProperty)             End Get             Set(ByVal Value As Brush)                 SetValue(ZuneColorProperty, Value)             End Set         End Property     End Class End Namespace 

Here is an example of how I use it currently

<Window x:Class='Demo'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:ui='clr-namespace:ZuneCards.Device'     Title='Demo' Height='300' Width='300' Name='Window1'>     <Grid>         <ui:Draco ZuneColour='Pink' HorizontalAlignment='Right' Margin='0,113,81,113' Width='20'></ui:Draco>     </Grid> </Window> 
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:14:41+00:00Added an answer on May 11, 2026 at 6:14 am

    1 . There is probably something else wrong if adding Name or x:Name causes the UserControl to fail. Moreover, use x:Name over Name.

    2 . The following code will use multiple controls:

    <WrapPanel>     <ui:Draco ZuneColour='Pink' Width='20' />     <ui:Draco ZuneColour='Red' Width='20' />     <ui:Draco ZuneColour='Orange' Width='20' /> </WrapPanel> 

    3 . A UserControl is a a class. The XAML for the UserControl is private unless exposed through templating. Therefore unless you provide methods or the consumer of the UserControl overrides the template, they cannot control the innards. The same goes for your UserControl, it can’t play with the innards of its parent. If you need that tight of coupling I suggest you not use a UserControl.

    4 . Given the following XAML:

    <StackPanel x:Name='ZunePanel' /> 

    You can use the following code behind:

    ZunePanel.Children.Add(new Draco()); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic cs-major understanding of multi-threading but have never had to do
We have had SharePoint where I work for a little while now, but we've
I have had to do this several times, usually when trying to find in
I have had a few problems with log files growing too big on my
I have had many requests come in to a project that I work on,
I have a project that I am working on and I can't figure out
Have had to write my first proper multithreaded coded recently, and realised just how
I have had a bug recently that only manifested itself when the library was
We have had issues with Mootools not being very backward compatible specifically in the
We have/had a phantom delay in our app. This was traced to the initialisation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.