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

C# 如何通过单击WinForms表单中的按钮来更改WPF表单中的标签背景色?,c#,.net,wpf,winforms,forms,C#,.net,Wpf,Winforms,Forms,在我的程序中,我同时打开了两个窗口,一个是WPF表单,另一个是WinForms表单 我需要通过单击WinForms窗体上的按钮来更改WPF窗体上的标签背景色 我该怎么做呢?我相信有很多方法可以做到这一点。有些更小,更模糊,有些更冗长,但更清晰 就像您可以通过win32 api做一些事情一样,通过内存映射文件进行通信,或者使用某种形式的非通信传输,如NamedPipes或MSMQ 您可以查看这些资源中的一些以获取想法: 祝你好运!:) 假设Winform对象在WPF表单中可用,并且Butt

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

我需要通过单击WinForms窗体上的按钮来更改WPF窗体上的标签背景色


我该怎么做呢?

我相信有很多方法可以做到这一点。有些更小,更模糊,有些更冗长,但更清晰

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

您可以查看这些资源中的一些以获取想法:


祝你好运!:)

假设Winform对象在WPF表单中可用,并且Button1是公共的,则需要在WPF表单中添加类似于下面的代码

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

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

当按下
WinForms
窗体中的
按钮
控件时,可以设置
EventHandler
来发布事件通知。此
事件
将由您的
WPF
窗口/用户控件
监控,以检测何时触发
事件
,然后请求所需的
颜色
WPF
标签设置为

main window.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

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));
            }
        }
    }
}
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);
            }
        }
    }
}
命名空间WpfApplication1
{
使用System.Windows;
使用系统组件模型;
使用System.Windows.Media;
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共主窗口()
{
初始化组件();
var f=新的Form1();
f、 更改颜色控制器+=触发器更改颜色控制器;
f、 Show();
}
私人画笔;
公共画笔标签颜色
{
得到
{
返回此.labelBgColour;
}
设置
{
this.labelBgColour=值;
OnPropertyChanged();
}
}
private void TriggeredChangedColourEventHandler(对象发送器,笔刷颜色)
{
this.LabelBgColour=颜色;
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName=null)
{
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
Form1.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));
            }
        }
    }
}
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);
            }
        }
    }
}
命名空间WpfApplication1
{
使用制度;
使用System.Windows.Forms;
使用System.Windows.Media;
公共部分类Form1:Form
{
public EventHandler changedcolorventhandler;
公共表格1()
{
初始化组件();
}
私人画笔颜色;
私人画笔颜色
{
得到
{
返回此.bg颜色;
}
设置
{
这个颜色=值;
触发器更改颜色控制器(值);
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
this.BgColour=(画笔)新画笔转换器().ConvertFromString(“#FF00B25A”);
}
专用无效触发器更改颜色控制器(笔刷颜色)
{
var handler=changedcolorventhandler;
if(处理程序!=null)
{
处理程序(此,颜色);
}
}
}
}
Form1
有一个名为
button1
按钮
控件(可以从示例cs中确定)。为了简洁起见,我省略了
Form1.Design.cs


此示例使用
WPF
窗口转到
WinForm
以获得所需的
颜色
值。它可以很容易地进行调整,以便
颜色
值作为
事件处理程序
事件(
EventArgs
)的一部分发送。

它们是同一个程序吗?如果是这样的话,你需要一种形式的对象引用在另一种形式的对象引用中,然后把值穿过去。是的,它是同一个程序。如果它们都是winforms表单,我可以使用form2.label1.backcolor,但我不知道如何更改WPF背景色。这种应用程序很有趣。我认为您应该在其中混合更多内容,例如
命令提示符
?让他们一起跳舞,你会玩得很开心:)我甚至不知道在同一个应用程序中可以有winform和WPF表单。这背后的原因是什么?你是否正常查看过更改WPF标签的背景?你应该显示你的代码,这样我们可以帮助你更好地假设/猜测/推测。我确信我们不需要任何类型的
IPC
,因为这两个窗口在同一个程序中。啊。。我发现这一点在我研究源代码并键入答案时得到了澄清。有了这个前提,你可以从创建类中钩住表单上的button clicked事件,让侦听器调用另一个表单中的方法。实际上,我在寻找的是你没有说的内容(//Change label Background)。我刚刚发现代码是wpform.label1.Background=System.Windows.Media.brusks.DarkRed;但我需要用一种特殊的颜色,比如说#ff00b25a你真的需要学会花更多的时间来表达你的问题,因为现在你已经有几个人浪费时间试图解决你没有的问题了。@user2558874请参阅我的答案以获得解决方案。请注意,只需让事件将颜色作为它触发的方法的参数传递,就可以稍微简化代码,这样处理程序就不需要返回到窗体中去处理g