Click here to Skip to main content
11,646,620 members (77,850 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
Hello,

I am developing an application with WPF that uses a 3rd party software. I`m struggling to make some label generated dynamically from an wpf user control and it seems that i`m not able to succeed.

Well, lets start.

I have an windows form that host an wpf user control. In that control i want to generate those labels and populate another windows form that pops up after the labels insertion.

The code looks something like this:

 
public void GetPropertyInfo(IvcComponent comp)
{
   int nr = comp.getPropertyCount;
   PropertyControl pc = new PropertyControl(); // this is the new win form
   for(int i=0;i<nr;i++)
   {
      Label x = new  Label();
      x.Name = comp.getPropertyName(i);
      x.Content = comp.getPropertyName(i);
      // here i should add the label to the PropertyControl form
   }
   pc.ShowDialog();
}
 
Posted 8-Jan-13 5:49am

1 solution

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

Solution 1

To start with, it's good to understand WPF content model: Question about MCTS exam[^].

First of all, you need to have some UIElement in your control which itself can have children, so you could add your label to it. Look at the abstract class System.Windows.Controls.Panel:
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.children.aspx[^].

You need to choose appropriate non-abstract derived class, to have a panel you can insert in your control:
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx#inheritanceContinued[^].

Typically, it will be Grid or DockPanel, and you will insert your label like myPanel.Children.Add(x). Location/layout and other properties of this parent-child relationships depends on the concrete panel class which you will be able to find in its documentation, but it's good to understand the functionality and the use of dependency properties:
http://msdn.microsoft.com/en-us/library/ms752914.aspx[^].

—SA
  Permalink  

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

  Print Answers RSS
0 jyo.net 510
1 Sergey Alexandrovich Kryukov 503
2 OriginalGriff 458
3 CPallini 329
4 Afzaal Ahmad Zeeshan 300
0 OriginalGriff 1,253
1 jyo.net 994
2 DamithSL 896
3 Sergey Alexandrovich Kryukov 833
4 CPallini 720


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2015
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