在.net3.0、3.5的WPF程序中虽然可以通过在References里添加对System.Windows.Forms库的引用访问2.0的控件,但是在WPF Xaml中却不能直使用2.0的控件,必须通过一个叫WindowsFormsHost的容器进行包装:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HostingWfInWpf"
>
<Grid>
<WindowsFormsHost Name="winFormsContainer">
</WindowsFormsHost>
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HostingWfInWpf"
>
<Grid>
<WindowsFormsHost Name="winFormsContainer">
</WindowsFormsHost>
</Grid>
</Window>
然后,你可以在后台代码中通过代码往host中添加控件:
System.Windows.Forms.Control ctrls = new System.Windows.Forms.Control()
winFormsContainer.Child = ctrls;
ctrls.Container.Add(new TextBox());
ctrls.Container.Add(new Label());
......
下面是MS的一个例子:
Xaml:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HostingWfInWpf" Height="300" Width="300"
Loaded="WindowLoaded"
>
<Grid Name="grid1">
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HostingWfInWpf" Height="300" Width="300"
Loaded="WindowLoaded"
>
<Grid Name="grid1">
</Grid>
</Window>
back code:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Forms;
namespace HostingWfInWpf
...{
public partial class Window1 : Window
...{
public Window1()
...{
InitializeComponent();
}
private void WindowLoaded(object sender, RoutedEventArgs e)
...{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the MaskedTextBox control.
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
// Assign the MaskedTextBox control as the host control's child.
host.Child = mtbDate;
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
}
}
}
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Forms;
namespace HostingWfInWpf
...{
public partial class Window1 : Window
...{
public Window1()
...{
InitializeComponent();
}
private void WindowLoaded(object sender, RoutedEventArgs e)
...{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the MaskedTextBox control.
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
// Assign the MaskedTextBox control as the host control's child.
host.Child = mtbDate;
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
}
}
}
相反,要在Windows Forms中使用WPF控件,就要用到ElementHost 。你可以在Forms里面添加一个Panel,然后再在这个Panel中添加WPF控件。下面是MS的一个例子:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Media;
namespace WFHost
...{
partial class Form1 : Form
...{
private ElementHost ctrlHost;
private MyControls.Page1 avAddressCtrl;
System.Windows.FontWeight initFontWeight;
double initFontSize;
System.Windows.FontStyle initFontStyle;
System.Windows.Media.SolidColorBrush initBackBrush;
System.Windows.Media.SolidColorBrush initForeBrush;
FontFamily initFontFamily;
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
ctrlHost = new ElementHost();
ctrlHost.Dock = DockStyle.Fill;
panel1.Controls.Add(ctrlHost);
avAddressCtrl = new MyControls.Page1();
avAddressCtrl.InitializeComponent();
ctrlHost.Child = avAddressCtrl;
avAddressCtrl.OnButtonClick +=
new MyControls.Page1.MyControlEventHandler(
avAddressCtrl_OnButtonClick);
avAddressCtrl.Loaded += new RoutedEventHandler(
avAddressCtrl_Loaded);
}
void avAddressCtrl_Loaded(object sender, EventArgs e)
...{
initBackBrush = (SolidColorBrush)avAddressCtrl.MyControl_Background;
initForeBrush = avAddressCtrl.MyControl_Foreground;
initFontFamily = avAddressCtrl.MyControl_FontFamily;
initFontSize = avAddressCtrl.MyControl_FontSize;
initFontWeight = avAddressCtrl.MyControl_FontWeight;
initFontStyle = avAddressCtrl.MyControl_FontStyle;
}
void avAddressCtrl_OnButtonClick(
object sender,
MyControls.MyControlEventArgs args)
...{
if (args.IsOK)
...{
lblAddress.Text = "Street Address: " + args.MyStreetAddress;
lblCity.Text = "City: " + args.MyCity;
lblName.Text = "Name: " + args.MyName;
lblState.Text = "State: " + args.MyState;
lblZip.Text = "Zip: " + args.MyZip;
}
else
...{
lblAddress.Text = "Street Address: ";
lblCity.Text = "City: ";
lblName.Text = "Name: ";
lblState.Text = "State: ";
lblZip.Text = "Zip: ";
}
}
private void radioBackgroundOriginal_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Background = initBackBrush;
}
private void radioBackgroundLightGreen_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Background = new SolidColorBrush(Colors.LightGreen);
}
private void radioBackgroundLightSalmon_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Background = new SolidColorBrush(Colors.LightSalmon);
}
private void radioForegroundOriginal_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Foreground = initForeBrush;
}
private void radioForegroundRed_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Foreground = new System.Windows.Media.SolidColorBrush(Colors.Red);
}
private void radioForegroundYellow_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Foreground = new System.Windows.Media.SolidColorBrush(Colors.Yellow);
}
private void radioFamilyOriginal_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_FontFamily = initFontFamily;
}
private void radioFamilyTimes_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_FontFamily = new FontFamily("Times New Roman");
}
private void radioFamilyWingDings_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_FontFamily = new FontFamily("WingDings");
}
private void radioSizeOriginal_CheckedChanged(object sender, EventArgs e)
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Media;
namespace WFHost
...{
partial class Form1 : Form
...{
private ElementHost ctrlHost;
private MyControls.Page1 avAddressCtrl;
System.Windows.FontWeight initFontWeight;
double initFontSize;
System.Windows.FontStyle initFontStyle;
System.Windows.Media.SolidColorBrush initBackBrush;
System.Windows.Media.SolidColorBrush initForeBrush;
FontFamily initFontFamily;
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
ctrlHost = new ElementHost();
ctrlHost.Dock = DockStyle.Fill;
panel1.Controls.Add(ctrlHost);
avAddressCtrl = new MyControls.Page1();
avAddressCtrl.InitializeComponent();
ctrlHost.Child = avAddressCtrl;
avAddressCtrl.OnButtonClick +=
new MyControls.Page1.MyControlEventHandler(
avAddressCtrl_OnButtonClick);
avAddressCtrl.Loaded += new RoutedEventHandler(
avAddressCtrl_Loaded);
}
void avAddressCtrl_Loaded(object sender, EventArgs e)
...{
initBackBrush = (SolidColorBrush)avAddressCtrl.MyControl_Background;
initForeBrush = avAddressCtrl.MyControl_Foreground;
initFontFamily = avAddressCtrl.MyControl_FontFamily;
initFontSize = avAddressCtrl.MyControl_FontSize;
initFontWeight = avAddressCtrl.MyControl_FontWeight;
initFontStyle = avAddressCtrl.MyControl_FontStyle;
}
void avAddressCtrl_OnButtonClick(
object sender,
MyControls.MyControlEventArgs args)
...{
if (args.IsOK)
...{
lblAddress.Text = "Street Address: " + args.MyStreetAddress;
lblCity.Text = "City: " + args.MyCity;
lblName.Text = "Name: " + args.MyName;
lblState.Text = "State: " + args.MyState;
lblZip.Text = "Zip: " + args.MyZip;
}
else
...{
lblAddress.Text = "Street Address: ";
lblCity.Text = "City: ";
lblName.Text = "Name: ";
lblState.Text = "State: ";
lblZip.Text = "Zip: ";
}
}
private void radioBackgroundOriginal_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Background = initBackBrush;
}
private void radioBackgroundLightGreen_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Background = new SolidColorBrush(Colors.LightGreen);
}
private void radioBackgroundLightSalmon_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Background = new SolidColorBrush(Colors.LightSalmon);
}
private void radioForegroundOriginal_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Foreground = initForeBrush;
}
private void radioForegroundRed_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Foreground = new System.Windows.Media.SolidColorBrush(Colors.Red);
}
private void radioForegroundYellow_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_Foreground = new System.Windows.Media.SolidColorBrush(Colors.Yellow);
}
private void radioFamilyOriginal_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_FontFamily = initFontFamily;
}
private void radioFamilyTimes_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_FontFamily = new FontFamily("Times New Roman");
}
private void radioFamilyWingDings_CheckedChanged(object sender, EventArgs e)
...{
avAddressCtrl.MyControl_FontFamily = new FontFamily("WingDings");
}
private void radioSizeOriginal_CheckedChanged(object sender, EventArgs e)