C#WPF添加控件... [英] C# WPF Adding controls.......

查看:131
本文介绍了C#WPF添加控件...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在普通的C#应用​​程序中添加控件

i know how to add a control in a normal C# app its

picturebox newpicturbox = new picturebox();
this.controls.add(newpicturbox);



但是如何在WPF中执行此操作?



but how do i do it in a WPF?

推荐答案

以相同的方式进行,仅使用适当的类/方法名称.

您必须将控件添加到容器对象(如网格,边框等).因此,如果您的表单具有名为"LayoutRoot"的网格,则可以执行以下操作:


The same way, only with the appropriate class/method names.

You have to add controls to a container object (like a grid, border, etc). So, if your form has a grid with the name "LayoutRoot", you''d do this:


this.LayoutRoot.Children.Add(myConrol);



我将创建控件并设置其属性,然后*添加*,但这只是我做的方法.



I would create the control and set its properties and *then* add it, but that''s just the way I do it.


如果您有一个容器",效果会更好例如边框或只是网格即可添加控件.

但是,如果您想以类似于Winforms的方式进行操作,这就是我尝试过的方法及其工作方式:

It will be better if you have a "container" like a Border or simply a Grid to add controls.

However if you want to do in a way similar to Winforms, here is how I tried and it worked:

BitmapImage btm = new BitmapImage(new Uri("Winter.jpg",UriKind.Relative));
            Image img = new Image();
            img.Source = btm;            
           
            this.AddChild(img);


WPF中没有图片框.


There is no picturebox in WPF.


这篇关于C#WPF添加控件...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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