【问题标题】:hosting stack panel in windows form以 Windows 形式托管堆栈面板
【发布时间】:2015-03-11 12:23:44
【问题描述】:

我的 Windows 窗体应用程序中有一个普通面板,我想在其中添加一个 WPF 中的堆栈面板。

堆栈面板内部应包含 2 个扩展器控件。

我正在使用 Element Host 在 windows 窗体中控制堆栈面板和扩展器。

我正在使用以下代码:

       ElementHost WPF_Container = new ElementHost();
        WPF_Container.AutoSize = true;

        ElementHost WPF_Container1 = new ElementHost();
        WPF_Container1 .AutoSize = true;

        ElementHost StackPanelHost = new ElementHost();

        Expander expander1 = new Expander();
        expander1.Header = "Pipes";

        Expander Expander2 = new Expander();
        Expander2.Header = "Mountings";

        uc1 uc1 = new uc1(); // Object Creation for User Control-1
        uc2 uc2 = new uc2();  // Object Creation for User Control-2

        StackPanel Stackpanel = new StackPanel();
        Stackpanel.Orientation = System.Windows.Controls.Orientation.Vertical;


        WindowsFormsHost host = new WindowsFormsHost();
        host.Child  = uc1 ;

        WindowsFormsHost host2 = new WindowsFormsHost();
        host2.Child = uc2;


        expander1.Content = host;
        Expander2.Content = host2;

        Stackpanel.Children.Add(expander1);
        Stackpanel.Children.Add(Expander2);


        // WPF_Container.Child = expander1;
        // WPF_Container1.Child = Expander2;
        StackPanelHost.Child = Stackpanel;

        panel1.Controls.Add(StackPanelHost); // Adding Stack Panel to my Winform Panel.

通过在表单加载事件期间使用上述代码,我的 Windows 表单面板中没有添加任何控件。 如何实现功能

【问题讨论】:

    标签: c# wpf winforms wpf-controls


    【解决方案1】:

    下面是一个例子:

    ElementHost host = new ElementHost();
    StackPanel wpfPanel = new StackPanel();
    host.Child = wpfPanel;
    //panel1 is Windows Forms Panel
    panel1.Controls.Add(host);
    

    我建议您在 WPF 用户控件中创建所需的 UI,然后将其添加到 ElementHost。

    更新:

    ElementHost host = new ElementHost();
    StackPanel wpfPanel = new StackPanel();
    host.Child = wpfPanel;
    //panel1 is Windows Forms Panel
    panel1.Controls.Add(host);
    var testButton = new System.Windows.Controls.Button(){Content = "test"};
    wpfPanel.Children.Add(testButton);
    

    注意您使用正确的命名空间作为窗口窗体以及按钮类

    【讨论】:

    • 我想在 WPF 堆栈面板中动态添加控件如何实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多