I recently had the need to read the foreground color of a WPF control to assign to the foreground color of a Windows Forms control that I had hosted in a Windows Forms Host control.  Once I started investigating, I realized that this wasn’t going to be as straight forward as I planned since the Foreground property of the WPF control was a DependencyProperty type and the ForeColor property of the Windows Forms control was a System.Drawing.Color type.

Through a little bit of research, I discovered that the Foreground property of the WPF control could be cast to a SolidColorBrush object if the Foreground was one solid color.  From there, I was able to extract the red, green, and blue components from the SolidColorBrush object.  I then created a new System.Drawing.Color object from these and assigned it to the ForeColor property of the Windows Forms control. See the example code below for how to achieve this:

System.Windows.Controls.Label wpfLabel = new System.Windows.Controls.Label();
wpfLabel.Foreground = Brushes.Red;
System.Windows.Forms.Label l = new System.Windows.Forms.Label();
SolidColorBrush b = (SolidColorBrush)wpfLabel.Foreground;
l.ForeColor = System.Drawing.Color.FromArgb(b.Color.R, b.Color.G, b.Color.B);

Hope this can save someone time in their coding efforts.

Converting From WPF Foreground to Windows Forms ForeColor
Tagged on:     

Leave a Reply