如何通过单击 WinForms 表单中的按钮来更改 WPF 表单中的标签背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19280765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:49:21  来源:igfitidea点击:

How can I change a label background color in a WPF form, by clicking a button in a WinForms form?

c#.netwpfwinformsforms

提问by user2558874

In my program I have two windows opened at the same time, one is a WPF form and the other one is a WinForms form.

在我的程序中,我同时打开了两个窗口,一个是 WPF 表单,另一个是 WinForms 表单。

I need to change a label background color that's on a WPF form, by clicking a button that's placed on the WinForms form.

我需要通过单击放置在 WinForms 表单上的按钮来更改 WPF 表单上的标签背景颜色。

How can I do it?

我该怎么做?

回答by

You could set up an EventHandlerto publish an event notification when the Buttoncontrol in your WinFormsform is pressed. This eventwould be monitored by your WPFWindow/UserControlto detect when the eventis triggered and then request the desired colorto set your WPFLabelto.

您可以设置在按下表单中EventHandlerButton控件时发布事件通知WinForms。这event将由您监控WPFWindow/UserControl以检测何时event触发,然后请求所需color的设置WPFLabel

MainWindow.xaml

主窗口.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="150" Width="225">
    <Grid>
        <Label Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="197" Background="{Binding LabelBgColour}"/>
    </Grid>
</Window>

MainWindow.xaml.cs

主窗口.xaml.cs

namespace WpfApplication1
{
    using System.Windows;
    using System.ComponentModel;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            var f = new Form1();
            f.ChangedColourEventHandler += TriggeredChangedColourEventHandler;
            f.Show();
        }

        private Brush labelBgColour;
        public Brush LabelBgColour
        {
            get
            {
                return this.labelBgColour;
            }
            set
            {
                this.labelBgColour = value;
                OnPropertyChanged();
            }
        }

        private void TriggeredChangedColourEventHandler(object sender, Brush color)
        {
            this.LabelBgColour = color;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

Form1.cs

表格1.cs

namespace WpfApplication1
{
    using System;
    using System.Windows.Forms;
    using System.Windows.Media;

    public partial class Form1 : Form
    {
        public EventHandler<Brush> ChangedColourEventHandler;

        public Form1()
        {
            InitializeComponent();
        }

        private Brush bgColour;
        private Brush BgColour
        {
            get
            {
                return this.bgColour;
            }
            set
            {
                this.bgColour = value;
                TriggerChangedColourEventHandler(value);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.BgColour = (Brush)new BrushConverter().ConvertFromString("#FF00B25A");
        }

        private void TriggerChangedColourEventHandler(Brush color)
        {
            var handler = ChangedColourEventHandler;
            if (handler != null)
            {
                handler(this, color);
            }
        }
    }
}

Form1has a single Buttoncontrol named button1(as can be determined from the example cs). I have omitted the Form1.Design.csfor brevity.

Form1有一个Button名为的单个控件button1(可以从示例 cs 中确定)。Form1.Design.cs为简洁起见,我省略了。

This example has the WPFwindow go to the WinFormto get the desired Colorvalue. It could be easily adapted so that the Colorvalue is sent as part of the EventHandlerevent (EventArgs).

此示例让WPF窗口转到WinForm以获取所需的Color值。它可以很容易地进行调整,以便将Color值作为EventHandler事件 ( EventArgs) 的一部分发送。

回答by Gaute L?ken

I'm sure there are a lot of ways to achieve this. Some smaller and more obscure and some more longwinded but more clear.

我相信有很多方法可以实现这一目标。有些更小更模糊,有些更冗长但更清晰。

Like you could do something through the win32 api, communicate through a memory-mapped file or use some form off communication transport like NamedPipes or MSMQ.

就像你可以通过 win32 api 做一些事情一样,通过内存映射文件进行通信或使用某种形式的非通信传输,如 NamedPipes 或 MSMQ。

You can look at some of these resources for ideas:

您可以查看其中一些资源以获得想法:

Best of luck! :)

祝你好运!:)

回答by monkxyz

Assuming that the Winform object is available in WPF form and the Button1 is public, you need to add code similar to below in the WPF form.

假设Winform对象在WPF窗体中可用,Button1是公共的,则需要在WPF窗体中添加类似下面的代码。

WinForm.Button1.Click += new System.EventHandler(Button1_Click);

private void Button1_Click(object sender, EventArgs e)
{
// Change label background
}