在WPF中动态创建usercontrol [英] Dynamically creating usercontrol in WPF

查看:99
本文介绍了在WPF中动态创建usercontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

这可能很简单但是到目前为止我还是坚持到了这里。

我想从代码后面动态创建用户控件并在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";



如果我创建一个用户控件它没有显示



C#代码:


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



任何人都可以解决问题PLZ。



谢谢


can any one solve the issue plz.

thanks

推荐答案

没有显示任何内容的原因是因为没有内容设置为 UserControl



以下代码将显示Hello:



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





只是再添加一个例子,假设您要添加图像进入 UserControl





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





更新:

来自kiranvls的评论:

嗨Tarun,在上面的代码中,用图像作为内容动态创建用户控件。现在我添加了一个事件处理程序u1.MouseMove + = new MouseEventHandler(uc_MouseMove); private void uc_MouseMove(object sender,MouseEventArgs e){这里iam无法访问动态创建的用户控件对象(u1);如何在事件处理程序中获取对象名称plz帮助我。



答案:

当然你可以访问它。查看MouseMove eventhanlder中的参数e。它的属性很丰富,包括Source属性。您必须将Source属性强制转换为UserControl。以下是你要做的事情:





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.


//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.


UserControl只是用于创建自己的控件的基本控件。除非您在UserControl中添加控件,否则无需显示。



希望这会有所帮助。
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.


这篇关于在WPF中动态创建usercontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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