首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MMC管理单元中的WPF

MMC管理单元中的WPF
EN

Stack Overflow用户
提问于 2010-04-25 10:53:28
回答 1查看 1.6K关注 0票数 5

有人能提供一些在自定义MMC管理单元中使用WPF的示例代码吗?我是WPF的新手,我已经了解了编写MMC管理单元的示例,但我不知道如何选择WPF而不是Winforms。

EN

回答 1

Stack Overflow用户

发布于 2010-05-15 05:56:10

我刚刚花了一早上的时间为我正在工作的一个项目构建了这个场景。以下是代码的上下文无关版本:

代码语言:javascript
复制
/// <summary>
/// Defines a scope node with a default Forms view that will host a
/// WPF control.
/// </summary>
public class WPFScopeNode: ScopeNode
{
    /// <summary>
    /// Instantiates a new specialized scope node.
    /// </summary>
    public WPFScopeNode()
        : base()
    {
        this.DisplayName = "Name in tree view";

        // TODO: Create any child scope nodes, adding them to 'this.Children'.

        // Attach a forms view to this node that hosts a WPF control.
        var wpfFormView = new FormViewDescription
        {
            DisplayName = "Name of view",
            ControlType = typeof(WPFHostControl),
            ViewType = typeof(WPFHostFormView)
        };
        this.ViewDescriptions.Add(wpfFormView );
    }
}


/// <summary>
/// This class provides a model-interface for the hosted WPF control.
/// </summary>
public class WPFHostFormView : FormView
{
    public WPFHostFormView ()
    {
        this.DescriptionBarText = "WPF Host View";
    }

    /// <summary>
    /// Defines the structure of the form view.
    /// </summary>
    /// <param name="status"></param>
    protected override void OnInitialize(AsyncStatus status)
    {
        base.OnInitialize(status);

        // TODO: This may be a good place to pass 'this.Control' the
        // data context that will be used for databinding by the WPF
        // control.  Note that 'this.Control' is the Windows Forms
        // UserControl that contains an ElementHost which, in turn,
        // hosts the WPF UserControl of interest.
    }
}


/// <summary>
/// Defines a Windows Forms control that hosts a specific WPF UserControl.
/// Note the logic for automatic resizing to the parent surface in the
/// MMC MDI interface.
/// Also, 'MyWPFUserControl' is just a standard WPF
/// UserControl; nothing special needs to be done there for MMC integration.
/// </summary>
public partial class WPFHostControl : UserControl
{
    // TODO: Expose a method for setting the DataContext of the hosted element!

    public WPFHostControl ()
    {
        InitializeComponent();

        ParentChanged += new EventHandler(WPFHostControl_ParentChanged);
    }

    void WPFHostControl_ParentChanged(object sender, EventArgs e)
    {
        if (Parent != null)
        {
            Size = Parent.ClientSize;
            Parent.ClientSizeChanged +=
                new EventHandler(Parent_ClientSizeChanged);
        }
    }

    void Parent_ClientSizeChanged(object sender, EventArgs e)
    {
        if (Parent != null)
        {
            Size = Parent.ClientSize;
        }
    }

    private MyWPFUserControl hostedControl;
    private System.Windows.Forms.Integration.ElementHost elementHost;

    /// <remarks>
    /// To make the Visual Studio designer work smoothly, you could
    /// split this function into a *.designer.cs file using a partial
    /// class definition.
    /// </remarks>
    private void InitializeComponent()
    {
        this.elementHost = new System.Windows.Forms.Integration.ElementHost();
        this.hostedControl = new MyWPFUserControl();
        this.SuspendLayout();
        // 
        // elementHost
        // 
        this.elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
        this.elementHost.Location = new System.Drawing.Point(0, 0);
        this.elementHost.Name = "elementHost";
        this.elementHost.Size = new System.Drawing.Size(150, 150);
        this.elementHost.TabIndex = 0;
        this.elementHost.Child = this.hostedControl;
        // 
        // EnvironmentDashboard
        // 
        this.Controls.Add(this.elementHost);
        this.Name = "WPF Host View";
        this.ResumeLayout(false);

    }
}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2706971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档