VARCHART XGantt in WPF-Anwendungen

Die Einbettung von XGantt .NET in WPF-Anwendungen

Auf die reichhaltige Funktionalität von XGantt braucht man in WPF-Anwendungen nicht zu verzichten. Microsoft bietet für die Integration von Windows Forms-Steuerelementen in WPF-Windows das WindowsFormsHost-Steuerelement an. Damit kann man die .NET Edition von XGantt auch innerhalb eines WPF-Windows verwenden. Im Folgenden werden zwei alternative Vorgehensweisen skizziert:

Voraussetzung: Hinzufügen der benötigten Referenzen

In beiden Fällen müssen für die Verwendung des WindowsFormsHost-Steuerelements die Referenzen auf WindowsFormsIntegration, System.Windows.Forms und natürlich auf NETRONIC.XGantt hinzugefügt werden.

Alternative 1: Integration über XAML-Code des WPF-Windows (Window1.xaml)

Bei diesem Ansatz wird XGantt innerhalb des XAML-Codes integriert:

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:ne="clr-namespace:NETRONIC.XGantt;assembly=NETRONIC.XGantt"

Title="Hosting XGantt .NET in WPF" Height="400" Width="500">

<Grid x:Name="grid1">

<WindowsFormsHost Margin="41,27,36,30" Name="windowsFormsHost1">

<ne:VcGantt Name="vcGantt1" Height="300" Width="450">

</WindowsFormsHost>

</Grid>

</Window>

Alternative 2: Integration über Code-Behind des WPF-Windows (Window1.xaml.cs)

Bei diesem Ansatz wird XGantt nicht über den XAML-Code sondern durch den Code-Behind in C# integriert. Deshalb beinhaltet der XAML-Code keine Besonderheiten; es handelt sich um die Standardbeschreibung eines WPF-Windows:

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Hosting XGantt .NET in WPF" Height="400" Width="500">

<Grid x:Name="grid1" />

</Window>

Im Code-Behind wird nun das WindowsFormsHost-Objekt angelegt, und diesem wird ein VcGantt-Objekt als Kind hinzugefügt:

using System.Windows.Forms.Integration;using NETRONIC.XGantt;namespace WpfApplication1

{

// Interaction logic for Window1.xaml


public partial class Window1 : Window

{

WindowsFormsHost windowsFormsHost1 = null;

VcGantt vcGantt1 = null;

public Window1()

{

InitializeComponent();

windowsFormsHost1 = new System.Windows.Forms.Integration.WindowsFormsHost();

vcGantt1 = new VcGantt();

windowsFormsHost1.Child = vcGantt1;

grid1.Children.Add(windowsFormsHost1);

}

}

}

 

Konfiguration über die Eigenschaftenseiten

Leider stehen bei beiden Alternativen zur Designzeit nicht die Eigenschaftenseiten von XGantt .NET zur Verfügung. Um dennoch nicht auf deren komfortable Kon­fi­gu­ra­tions­mög­lich­kei­ten verzichten zu müssen, empfehlen wir, innerhalb des Projektes noch eine Windows Form anzulegen. Auf diese Form kann man nun eine weitere XGantt-Instanz platzieren und dort die gewünschten Einstellungen über die Eigenschaftenseiten vornehmen. Die so entstandende Kon­fi­gu­ration kann dann als *.ini-Konfigurationsdatei exportiert und anschließend wieder in das VcGantt-Objekt des WPF-Windows importiert werden:

public Window1()

{

InitializeComponent();

VcGantt vcGantt1 = (VcGantt) windowsFormsHost1.Child; //nur mit Alternative 1 benötigt

vcGantt1.InitializeForWebService();

vcGantt1.ImportConfiguration("Name_of_Configuration_file.ini");

}

Natürlich können auf gleiche Weise auch die NETRONIC-Komponenten XNet .NET und XTree .NET in WPF-Applikationen eingebettet werden.

Hilfreiche Links