how to reference the parent form from the WPF control(Control in ElementHost)

Winform 中放置 ElementHost 控件加载 wpf 的 UserControl

在 WPF UserControl 中获取 winform 的方法

    public partial class CrlDefinition : UserControl
    {
        public CrlDefinition()
        {
            InitializeComponent();
this.Loaded += CrlDefinition_Loaded;
        }

        void CrlDefinition_Loaded(object sender, RoutedEventArgs e)
        {
            var source = (HwndSource)PresentationSource.FromDependencyObject(this);
            var host = (System.Windows.Forms.Integration.ElementHost)System.Windows.Forms.Control.FromChildHandle(source.Handle);
            var form = (System.Windows.Forms.Form)host.TopLevelControl;
            // Show form title
            MessageBox.Show(form.Text);
        }

 

posted @ 2013-03-24 11:38  I'm CY  阅读(360)  评论(0编辑  收藏  举报