Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
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 7-Jul-11 2:31am
Edited 7-Jul-11 2:33am
v2
Comments
David Eyal at 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;

}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The reason why nothing is displayed is because there is no content set to your UserControl.
 
The below code will display Hello:
 
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.
 
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:
 
void uc_MouseMove(object sender, MouseEventArgs e)
{
    // Here is your UserControl.
    UserControl uControl = (UserControl)e.Source;
}
 
Hope this helps.
  Permalink  
v5
Comments
kiranvls at 7-Jul-11 8:46am
   
Working perfectly..
Exactly wat i needed..
thanku..
Tarun K.S at 7-Jul-11 8:58am
   
Glad it helped!
kiranvls at 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 at 8-Jul-11 4:01am
   
I have updated the answer. Check now.
Christian Graus at 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.
Tarun K.S at 8-Jul-11 5:14am
   
Sure, I will do that next time.
Uday P.Singh at 7-Jul-11 8:57am
   
my 5!
Tarun K.S at 7-Jul-11 8:59am
   
Thank you!
SAKryukov at 7-Jul-11 15:10pm
   
No content? -- good catch. My 5.
--SA
Tarun K.S at 7-Jul-11 16:34pm
   
Thanks a lot SA!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

//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.
  Permalink  
v3
Comments
kiranvls at 7-Jul-11 8:48am
   
thanku.
now i understood clearly
Uday P.Singh at 7-Jul-11 8:57am
   
my 5!
SAKryukov at 7-Jul-11 15:12pm
   
Same thing -- correct -- a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
kiranvls at 7-Jul-11 8:47am
   
thanks for the reply.
good point to notice.
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 8 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100