Amr H. Abd Elmajeed
Amr H. Abd Elmajeed

Reputation: 1521

Create Silverlight custom control with code only (no xaml)

I would like to create a Silverlight custom control using C# only, without any xaml.

Here is my work so far (stripped down to the bare minimum for the question):

I tried to inherit User control as follows:

public class myControl: UserControl
{
    // class code
}

And adding it to the LayoutRoot:

myControl control = new myControl();
LayoutRoot.Children.Add(control);

The control is added, but its invisible!!

How can i make it visible ? Is there something i missed ?

edit: The only visual element in my contorl is a grid with an image background

Upvotes: 1

Views: 603

Answers (2)

Dean Chalk
Dean Chalk

Reputation: 20461

Your Usercontrol will be empty and have no visual effect until you give it a child control via it's Content property.

Upvotes: 4

dain
dain

Reputation: 6689

Well unless you put a template in place or add elements in code, UserControl is empty.

Maybe you could try inheriting from an existing control which has a template, like Button, etc and change that in code?

Upvotes: 1

Related Questions