Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,
This may be simple but as of now i stuck up here.
I want to create user control dynamically from code behind and display it in WPF window.
if i click on a button it should create usercontrol and display.
so, in the button click event if i create a button or label dynamically it is working fine.

eg:
Button btn=new Button();
btn .Height =25;
btn.Width=80;
btn.Content="codebutton";

if i create a user Control it is not displaying

C# code:
UserControl u1=new UserControl();
u1.Height=100;
u1 .Width=100;
uniformgrid1 .Children .Add (u1);

can any one solve the issue plz.

thanks
Posted
Updated 7-Jul-11 1:33am
v2
Comments
David Eyal 1-Dec-11 8:09am    
Hi all,
I've been trying to do the same - only by creating an array of 5 labels.
I can't see the labels during run time. Can any1 find me the reason why?
Thank you so much on advance!
Eyal,
Here is my code:

public MainWindow()
{ // Label Height="28" HorizontalAlignment="Left" Margin="221,98,0,0" Name="lblScoreD6" VerticalAlignment="Top" Width="100" Content="0" />
InitializeComponent();
Label[] scoresLabelArr = new Label[5];
int location = 98;
for (int i = 0; i < scoresLabelArr.Length; i++)
{
scoresLabelArr[i] = new Label();
scoresLabelArr[i].Height = 28;
scoresLabelArr[i].Width = 100;
scoresLabelArr[i].HorizontalAlignment = HorizontalAlignment.Left;
scoresLabelArr[i].VerticalAlignment = VerticalAlignment.Top;
scoresLabelArr[i].Content = "0";
scoresLabelArr[i].Margin = new Thickness(211, location, 0, 0);
scoresLabelArr[i].Foreground = Brushes.Black;
// In the designer - i've named the Grid [grid1]
grid1.Children.Add((Label)scoresLabelArr[i]);
location += 34;

}

C#
//Create the button
Button btn = new Button();
btn .Height = 25;
btn.Width = 80;
btn.Content = "codebutton";

// create a container for ithe button
Grid grid = new Grid();
// add the button to the container
grid.Children.Add(btn);

// create the user control
UserControl u1 = new UserControl();
u1.Height = 100;
u1 .Width = 100;
// set the content to be the grid you created earlier
u1.Content = grid;

// put the user control into the uniformgrid

uniformgrid1.Children.Add(u1);


EDIT ==============

BTW, when I create controls dynamically like that, I make it a practice to create the controls, and then their containers, and then finally add the containers to the parent containers. that way, I can clearly see the parent/child relationship chain with a quick glance at the code.
 
Share this answer
 
v3
Comments
KiranBabu M 7-Jul-11 8:48am    
thanku.
now i understood clearly
Uday P.Singh 7-Jul-11 8:57am    
my 5!
Sergey Alexandrovich Kryukov 7-Jul-11 15:12pm    
Same thing -- correct -- a 5.
--SA
The reason why nothing is displayed is because there is no content set to your UserControl.

The below code will display Hello:

C#
UserControl u1 = new UserControl();
u1.Height = 100;
u1.Width = 100;
u1.Content = "Hello"
uniformgrid1.Children.Add(u1);


Just to add one more example, let's say you want to add an Image into your UserControl.

C#
UserControl u1 = new UserControl();
u1.Height = 100;
u1.Width = 100;
Image img = new Image();
img.Source = new BitmapImage(new Uri(@"C:\Windows.png",UriKind.RelativeOrAbsolute));
u1.Content = img;
uniformgrid1.Children.Add(u1);


Update :
Comment from kiranvls:
Hi Tarun, In the above code user control is dynamically created with image as content. Now i have added a event handler u1.MouseMove +=new MouseEventHandler(uc_MouseMove); private void uc_MouseMove(object sender, MouseEventArgs e) { Here iam unable to access the dynamically created user control object(u1); } how to get object name in event handler plz help me.

Answer:
Sure you can access it. Check out the argument "e" in the MouseMove eventhanlder. It's rich in properties including the Source property. You have to cast the Source property to UserControl. Here is what you have to do:

C#
void uc_MouseMove(object sender, MouseEventArgs e)
{
    // Here is your UserControl.
    UserControl uControl = (UserControl)e.Source;
}


Hope this helps.
 
Share this answer
 
v5
Comments
KiranBabu M 7-Jul-11 8:46am    
Working perfectly..
Exactly wat i needed..
thanku..
Tarun.K.S 7-Jul-11 8:58am    
Glad it helped!
KiranBabu M 8-Jul-11 1:52am    
Hi Tarun,
In the above code user control is dynamically created with image as content. Now i have added a event handler
u1.MouseMove +=new MouseEventHandler(uc_MouseMove);

private void uc_MouseMove(object sender, MouseEventArgs e)
{
Here iam unable to access the dynamically created user control object(u1);
}
how to get object name in event handler plz help me.
Tarun.K.S 8-Jul-11 4:01am    
I have updated the answer. Check now.
Christian Graus 8-Jul-11 4:07am    
Please don't answer new questions like this, they are not indexed by the site. I gave him the same answer where he posted a new question and I bet now he won't mark it, and it will be lost. If he had just asked it here, it would not be in the index at all. Tell people to post their new question and answer it there.
A UserControl is just a base control for creating your own controls. Unless you add controls into the UserControl, there is nothing to show.

Hope this helps.
 
Share this answer
 
Comments
KiranBabu M 7-Jul-11 8:47am    
thanks for the reply.
good point to notice.
Sergey Alexandrovich Kryukov 7-Jul-11 15:11pm    
Yes, good point, enough to explain the problem; a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900