Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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
 
Share this answer
 

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