使用WPF用户控件和属性 [英] working with WPF user controls and properties

查看:344
本文介绍了使用WPF用户控件和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是WPF的新手。我正在创建一个WPF用户控件。我使用Element Host在Win Forms应用程序中托管此用户控件。现在我遇到了以下问题。



我想为每个WPF用户控件对象分配一个特定的id。为此,我在控件中创建了一个Id变量,并创建了一个Property来获取和设置Id值。但是在我的Win Form中,我创建了WPF用户控件对象,我无法查看属性(它是一个公共属性)。



这里是我的WPF用户控件代码。



Hi every one,

I am new to WPF. I am creating a WPF user control. I am hosting this user control in Win Forms application using Element Host. Now i am getting following problem.

I want to assign a specific id to each of the WPF user control object. For that i created an Id variable in the control and created a Property to get and set Id value. But in my Win Form where i create the WPF user control object i can not view the property (it is a public property).

here is my WPF user control code.

public partial class MyWPFControl: UserControl
{

       public ucWPFContainer()
       {
           InitializeComponent();
       }

       private int Id;

       #region Properties

       public int Id
       {
           get
           {
               return this.Id;
           }
           set
           {
               this.Id = value;
           }
       }

      /*
      other code
      */
}





这是我的Win Form代码





and here is my Win Form code

form_Load(object sender, EventArgs e)
{
     MyWPFControl obj = new MyWPFContro();
     
     obj. // [What is that? Is it a part of code? — SA]
}





现在ob.Id此处未显示。



请告诉我我错在哪里。



提前感谢您的时间。



now ob.Id is not shown here.

Please tell me where i am wrong.

Thanks in advance for your time.

推荐答案

显然,这是因为你只创建了一些对象并且没有做任何其他事情。应用程序如何知道您想要显示它?您需要在其父内容控件中添加控件的实例。



请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol。 content.aspx [ ^ ],

http: //msdn.microsoft.com/en-us/library/system.windows.window.aspx [ ^ ]( Window 是内容控件之一。



您可以直接在窗口中插入控件实例,这样您的问题就会得到解答,但在实践中看起来不太好或者特别有用。 ,窗口的内容是某种类型的面板。并且面板允许添加几个控件,因为它具有类型 UIElementCollection 的属性 Children

http://msdn.microsoft.com/en -us / library / system.windows.controls.panel.children.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.aspx [ ^ ]。



所以,你需要添加插入你的控件实例:

http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.add.aspx [ ^ ],

http://msdn.microsoft.com/en -us / library / system.windows.controls.uielementcollection.insert.aspx [ ^ ]。



你应该首先理解的是WPF内容型号: http://msdn.microsoft.com/en-us/library/bb613548.aspx [ ^ ]。



这应该向您解释代码应该做什么。但是,您也可以在XAML中添加一些控件,包括您的控件。使用XAML,在项目构建期间将自动生成执行相同操作的代码。此自动生成的代码不是项目的一部分,这是一组临时* .cs文件。如果要查看此代码的外观,可以在项目子目录的目录结构中找到。构建后不会删除它们。所以,这是我的建议,看看事情是如何工作的:用XAML创建一个项目,构建它,找出自动生成的* .cs文件,看看它们写的是什么。通过这种方式,你可以学到很多基础知识。



-SA
Apparently, this is because you only created some object and did not do anything else. How a application can "know" that you want to show it? You need to add an instance of your control in its parent content control.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.window.aspx[^] (Window is one of content controls.

You would be able to insert an instance of your control in the window directly, so your question would be answered, but it won't look good or be especially useful in practice. Normally, the content of a window is of some type of panels. And a panel would allow to add several controls, because it has the property Children of the type UIElementCollection:
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.children.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.aspx[^].

So, you would need to Add or Insert the instance of your control:
http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.add.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.insert.aspx[^].

First thing you should understand in general is the WPF content model: http://msdn.microsoft.com/en-us/library/bb613548.aspx[^].

This should explain to you what to code should do. But also, you can simply add some controls, including yours, in XAML. With XAML, the code doing the same will be auto-generated during build of the project. This auto-generated code is not part of the project, this is a set of temporary *.cs files. If you want to see how this code looks like, you can find then inside the directory structure of your project sub-directory. They are not deleted after the build. So, this is my advice to see how things work: create a project with XAML, build it, find out auto-generated *.cs files and see what is written in them. This way, you can learn a lot of basics.

—SA


你好,< br $>


您缺少控件类名称,MyWpfControl是命名空间,UserControl是控件类。



试试这个< br $>
form_Load(对象发送者,EventArgs e)

{

MyWPFControl.UserControl obj = new MyWPFControl.UserControl();



var r = obj.Id;

}



我希望你能解决问题。



谢谢

Mohit
Hi,

You are missing Control class name, MyWpfControl is namespace and UserControl is control class.

Try this
form_Load(object sender, EventArgs e)
{
MyWPFControl.UserControl obj = new MyWPFControl.UserControl() ;

var r= obj.Id;
}

I hope that you got solution of your problem.

Thanks
Mohit






i不明白我问过一个非常简单的问题在我的WinForm类中,我想为WPF用户控件对象的Id属性分配一些值。正如我在代码中所示



Hi,

i do not understand i have asked a very simple question that in in my WinForm class i want to assign some value to the Id property of the WPF user control object. as i have shown in the code

form_Load(object sender, EventArgs e)
{
     MyWPFControl obj = new MyWPFControl();
     
     obj. [here i want to assign some value to object like obj.Id = 2]
}





但Visual Studio Intellisence是我输入obj时没有显示Id属性。并且没有添加引用的问题,因为在第一行中成功创建了对象。



but Visual Studio Intellisence is not showing the Id property when i type obj. and there is no issue of adding reference as object is created successfully in the first line.


这篇关于使用WPF用户控件和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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