You could set up an EventHandler
to publish an event notification when the Button
control in your WinForms
form is pressed. This event
would be monitored by your WPF
Window/UserControl
to detect when the event
is triggered and then request the desired color
to set your WPF
Label
to.
MainWindow.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));
}
}
}
}
Form1.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);
}
}
}
}
Form1
has a single Button
control named button1
(as can be determined from the example cs). I have omitted the Form1.Design.cs
for brevity.
This example has the WPF
window go to the WinForm
to get the desired Color
value. It could be easily adapted so that the Color
value is sent as part of the EventHandler
event (EventArgs
).