Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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.

C#
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
      */
}


and here is my Win Form code

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


now ob.Id is not shown here.

Please tell me where i am wrong.

Thanks in advance for your time.
Posted
Updated 11-Oct-13 5:26am
v5
Comments
Richard MacCutchan 11-Oct-13 9:24am    
Have you added a link to the control's namespace in your XAML?
Sergey Alexandrovich Kryukov 11-Oct-13 11:44am    
The problems is: OP does not use XAML here, which is fine, but the object just needs to be added to the UI.
Please see my answer.
—SA
Sergey Alexandrovich Kryukov 11-Oct-13 11:45am    
Next time, please format your code with "pre" tags.
—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
 
Share this answer
 
v2
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
 
Share this answer
 
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]
}


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.
 
Share this answer
 
Comments
Jibesh 14-Oct-13 19:52pm    
make sure that the property is public and you build the control after making the changes and try getting the properties in your client control or class.

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