从wpf用户控件动态创建标签 [英] Create labels dynamically from a wpf user control

查看:117
本文介绍了从wpf用户控件动态创建标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在开发一个使用第三方软件的WPF应用程序。我很难从wpf用户控件动态生成一些标签,似乎我无法成功。



嗯,让我们开始吧。



我有一个托管wpf用户控件的Windows窗体。在该控件中,我想生成这些标签并填充标签插入后弹出的另一个窗体。



代码如下所示:



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();
}

推荐答案

首先,了解WPF 内容模型是件好事:有关MCTS考试的问题 [ ^ ]。



首先,你需要一些<$ c你控制中的$ c> UIElement 本身可以有孩子,所以你可以添加你的标签。查看抽象类 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 [ ^ ]。



您需要选择合适的非抽象派生类,有一个面板,你可以插入你的控件:

http://msdn.microsoft.com/en-us/library/system.wi ndows.controls.panel.aspx#inheritanceContinued [ ^ ]。



通常,它将是 Grid DockPanel ,您将插入您的标签,如 myPanel.Children.Add(x)。此父子关系的位置/布局和其他属性取决于您可以在其文档中找到的具体面板类,但理解依赖属性的功能和使用

http://msdn.microsoft.com/en-us /library/ms752914.aspx [ ^ ]。



-SA
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


这篇关于从wpf用户控件动态创建标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆