none
请教:wpf和winform混合 RRS feed

  • 问题

  • 为了更好的显示效果,想把主界面用WPF来做,其他的窗口用Winform,不知道是否可行?如果可行的话哪里有示例代码?谢谢了!
    2013年12月17日 1:45

答案

  • 您好!

    这个是可行的。我们可以在WPF中承载Windows Forms Application的控件,可以参考下面的一个Walkthrough,

    http://msdn.microsoft.com/en-us/library/ms751761(v=vs.110).aspx

    首先我们需要引用下面两个namespace.

    WindowsFormsIntegration

    System.Windows.Forms

    我在本地创建了一个WPF的项目来host Windows Forms Application的控件,

    XAML Code:

    <Window x:Class="WPFHostWFControl.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <Grid>
            <WindowsFormsHost x:Name="winFormHost" Margin="0,0,0,0">
                
            </WindowsFormsHost>
        </Grid>
    </Window>

    C# Code:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                System.Windows.Forms.TextBox textbox = new System.Windows.Forms.TextBox();
                textbox.Text = "Hello WPF!";
                winFormHost.Child = textbox;
            }
        }

    这里有一些相关资料:

    #WPF and Windows Forms Interoperation

    http://msdn.microsoft.com/en-us/library/ms751797(v=vs.110).aspx

    #Host Windows Form Controls in WPF

    http://www.codeproject.com/Tips/130414/Host-Windows-Form-Controls-in-WPF

    祝您生活愉快!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 FrankYuan_1 2013年12月18日 3:10
    2013年12月17日 8:48
    版主

全部回复

  • 您好!

    这个是可行的。我们可以在WPF中承载Windows Forms Application的控件,可以参考下面的一个Walkthrough,

    http://msdn.microsoft.com/en-us/library/ms751761(v=vs.110).aspx

    首先我们需要引用下面两个namespace.

    WindowsFormsIntegration

    System.Windows.Forms

    我在本地创建了一个WPF的项目来host Windows Forms Application的控件,

    XAML Code:

    <Window x:Class="WPFHostWFControl.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <Grid>
            <WindowsFormsHost x:Name="winFormHost" Margin="0,0,0,0">
                
            </WindowsFormsHost>
        </Grid>
    </Window>

    C# Code:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                System.Windows.Forms.TextBox textbox = new System.Windows.Forms.TextBox();
                textbox.Text = "Hello WPF!";
                winFormHost.Child = textbox;
            }
        }

    这里有一些相关资料:

    #WPF and Windows Forms Interoperation

    http://msdn.microsoft.com/en-us/library/ms751797(v=vs.110).aspx

    #Host Windows Form Controls in WPF

    http://www.codeproject.com/Tips/130414/Host-Windows-Form-Controls-in-WPF

    祝您生活愉快!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 FrankYuan_1 2013年12月18日 3:10
    2013年12月17日 8:48
    版主
  • 谢谢你的回答!
    2013年12月18日 3:11