WpfControl in WindowsForms benutzen

  • VB.NET

Es gibt 11 Antworten in diesem Thema. Der letzte Beitrag () ist von Mangafreak1995.

    WpfControl in WindowsForms benutzen

    Hi.

    Ich würde gerne ein Control ( neues Element hinzufügen => WpfUserControl ) das ich mit wpf verändrt habe (grafisch)
    als dll exportieren, mit der auch Windows Forms was anfangen kann!
    Geht das?

    mfg,
    GreenVB-user
    Soweit ich wie ich das weiß geht das nur andersherum.
    Ganz einfach:

    Erst ein WPF-Usercontrol erstellen, das die WPF-Elemente einhalten soll. Dann auf die Windows-Form ein ElementHost packen, und im Smarttag das Usercontrol wählen.
    Ich war mal so frei und hab den C#-Code schon mal übersetzt:
    Spoiler anzeigen

    Previously I discussed about Hosting Windows Forms Controls in Windows Presentation Foundation. This is a more common scenario but in opposite direction you may want to host WPF controls in Windows Forms applications. This is easy, too and I'll talk about it in this post.

    Like hosting a Windows Forms control in WPF where you could put a WindowsFormsHost element to host your Windows Forms control inside it, you must put an ElementHost control on your Windows Form to host your WPF control.

    On the other hand you must add four references to PresentationCore, PresentationFramework, WindowsBase and WindowsFormsIntegration assemblies. You can remember the last one for hosting Windows Forms controls in WPF.

    Like developing for WPF you can create your WPF controls and you have access to their properties and methods but you need to create an ElementHost control to keep these WPF controls on Windows Form and show them. By adding this ElementHost control to your Windows Form you'll be able to get benefits of WPF controls in your Windows Forms applications.

    As an example I create a simple Windows Forms application that hosts a WPF TextBox control with a default text and shows a MessageBox whenever user changes the text in this TextBox.

    To do this I add required references to my solution and code.

    VB.NET-Quellcode

    1. 'Imports System
    2. 'Imports System.Collections.Generic
    3. 'Imports System.ComponentModel
    4. 'Imports System.Data
    5. 'Imports System.Drawing
    6. 'Imports System.Text
    7. 'Imports System.Windows.Forms
    8. ' Die auskommentierten Imports werden nicht benötigt, da entweder nicht verwendet oder schon von Visual Basic vorher importiert in einem WinForms-Projekt
    9. Imports System.Windows.Forms.Integration
    10. Imports System.Windows.Controls
    11. Public Class frmMain

    My Windows Form has a Panel control named containerPanel which is added to keep my ElementHost control. Now in form initialization I add my main code to create a WPF TextBox on fly and set some properties and event handlers for it. Then create an ElementHost object and add my TextBox as its Child property. At the end I add this ElementHost control to my Panel to show it on my Windows Form.

    Visual Basic-Quellcode

    1. Public Class frmMain
    2. Public Sub New()
    3. InitializeComponent()
    4. Dim wpfTextBox As New System.Windows.Controls.TextBox()
    5. ' Ja man muss hier den vollen Namespace angeben, da es ja auch eine Klasse Textbox gibt im Namespace System.Windows.Forms
    6. wpfTextBox.Name = "myTextBox"
    7. wpfTextBox.Text = "WPF TextBox"
    8. AddHandler wpfTextBox.TextChanged, AddressOf(textbox_TextChanged)
    9. ElementHost elementHost = new ElementHost()
    10. elementHost.Dock = DockStyle.None
    11. elementHost.Width = 150
    12. elementHost.Height = 50
    13. elementHost.Child = wpfTextBox
    14. Me.Controls.Add(elementHost)
    15. End Sub
    16. Private Sub textbox_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
    17. MessageBox.Show("Text Changed!", ":-D")
    18. End Sub
    19. End Class

    Now if I run this application, a Windows Presentation Foundation TextBox appears on my Windows Form. If I change the text in TextBox then a MessageBox will be shown to inform me about a change in text.



    Alles ist im Kopf übersetzt, ich gebe keine Garantie.
    naja was heißt einfacher ? es ist im Endeffekt auch nur Code, aber es ist einfacher es handzuhaben da man mit Klickibunti weitermachen kann ;) xD
    Danke erstmal.
    Das Einbinden hat super geklappt.
    Aber wie kann ich jetzt per Code in Public Class Form1 das Element (z.B. eine Progressbar) bearbeiten?

    VB.NET-Quellcode

    1. Progressbar1.Increment(1)
    2. Progressbar1.Value += 1
    3. (...)

    Geht das irgendwie?

    gruß,
    GreenVB-user
    Ich würde sagen :

    VB.NET-Quellcode

    1. CType(WPFHost.Child, Controls.Progressbar).Increment(1) ' wenns im WPF die Prozedur auch gibt ^^

    um dir sowas zu ersparen kannst du dir ja eine Eigenschaft machen :

    VB.NET-Quellcode

    1. Private Property WPFProgressbar As Controls.Progressbar
    2. Get
    3. Return CType(WPFHost.Child, Controls.Progressbar)
    4. End Get
    5. Set (ByVal value As Controls.Progressbar)
    6. WPFHost.Child = value
    7. End Set
    8. End Property