【问题标题】:how to change the layout of a control inside a parent wpf form from a child winform?如何从子winform更改父wpf表单中控件的布局?
【发布时间】:2013-05-03 10:00:05
【问题描述】:

我在一个解决方案中有两个独立的项目,一个是 wpf,另一个是 windows 窗体,我已将 winform 引用到 wpf 项目。wpf 窗口内是一个图像控件,单击时,一个带有按钮会出现。

单击winform中的按钮时,我如何能够更改wpf表单中图像控件的图像源...

我见过similar question to this ,但我无法理解答案...

【问题讨论】:

    标签: wpf winforms parent-child


    【解决方案1】:

    您可以将委托/操作传递到 Winform 以执行操作

    这是一个非常简单的例子

    WPF

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
           // pass in the method you want to call when the winform button is clicked
           var winform = new Form1(() => ChangeImage()).ShowDialog();
        }
    
        private void ChangeImage()
        {
            // your change image logic
        }
    }
    

    Winforms

    public partial class Form1 : Form
    {
        private Action _action;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        public Form1(Action action)
        {
            _action = action;
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (_action != null)
            {
                // call the method in the WPF form
                _action.Invoke();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2018-08-17
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多