Search related threads. Remove From My Forums. Answered by:. Archived Forums. Sign in to vote. Thanks in advance. Wednesday, January 23, AM. Forms ; Add the namespace of the UserControl. Host it into WindowsFormsHost. I made a code sample here: UserControl file: using System; using System. Generic; using System.
Linq; using System. The composite control is packaged in a DLL. This general procedure can be extended to more complex applications and controls.
The primary difference is that the hosting scenario is reversed. The walkthrough is divided into two sections. The first section briefly describes the implementation of the WPF composite control. The second section discusses in detail how to host the composite control in a Windows Forms application, receive events from the control, and access some of the control's properties. The WPF composite control used in this example is a simple data-entry form that takes the user's name and address.
When the user clicks one of two buttons to indicate that the task is finished, the control raises a custom event to return that information to the host.
The following illustration shows the rendered control. For the location, specify a conveniently named top-level folder, such as WindowsFormsHostingWpfControl. Later, you will put the host application in this folder. Click OK to create the project. The default project contains a single control named UserControl1.
Your project should have references to the following system DLLs. If any of these DLLs are not included by default, add them to your project. The composite control UI consists of five TextBox elements. Each TextBox element has an associated TextBlock element that serves as a label.
There are two Button elements at the bottom, OK and Cancel. When the user clicks either button, the control raises a custom event to return the information to the host.
The various UI elements are contained in a Grid element. You can use Grid to arrange the contents of the composite control in much the same way you would use a Table element in HTML. WPF also has a Table element, but Grid is more lightweight and better suited for simple layout tasks.
The following XAML shows the basic layout. This XAML defines the overall structure of the control by specifying the number of columns and rows in the Grid element. You place a UI element in the grid by setting the element's RowProperty and ColumnProperty attributes to the appropriate row and column number.
Remember that row and column numbering are zero-based. You can have an element span multiple columns by setting its ColumnSpanProperty attribute. For more information about Grid elements, see Create a Grid Element. In MyControl1. Many of the elements on the data-entry form have a similar appearance, which means that they have identical settings for several of their properties.
Rather than setting each element's attributes separately, the previous XAML uses Style elements to define standard property settings for classes of elements. This approach reduces the complexity of the control and enables you to change the appearance of multiple elements through a single style attribute.
The Style elements are contained in the Grid element's Resources property, so they can be used by all elements in the control. If a style is named, you apply it to an element by adding a Style element set to the style's name. Styles that are not named become the default style for the element. To see how the styles are applied to elements, see the previous XAML. For example, the last TextBlock element has the inlineText style, and the last TextBox element uses the default style.
The final elements on the composite control are the OK and Cancel Button elements, which occupy the first two columns of the last row of the Grid. The XAML part of the composite control is now complete. Retrieves the data from the TextBox elements, and packages it in a custom event argument object. Raises the custom OnButtonClick event, which notifies the host that the user is finished and passes the data back to the host.
While I personally love working with WPF, upgrading to HTML5 will get you a multi-platform web application at a lower cost, especially if you do it automatically using transposition tools. I've done many of these projects and it is certainly easier than re-writing it to WPF. Also, creating complicated controls in WPF usually takes less time than creating them in Windows Form. What I present here is an easy example of hosting a WPF control inside Windows Forms application in a way that is simple to follow and implement, and is also more suitable from the perspective of project architecture.
0コメント