سؤال

I'm creating a WPF 'Button' control and making it the child of my ElementHost control.

The background of the button is set to Red.

When I run my project, the button changes colour and it seems to loop through every couple of seconds going from Red to a light blue and back again.. until the Form loses focus.

If I hover over the button, it turns blue, then when I move off the button it starts this colour looping again.. my code is as simple as..

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        System.Windows.Controls.Button but = new System.Windows.Controls.Button();
        but.Background = System.Windows.Media.Brushes.Red;
        elementHost1.Child = but;
    }
}

Is this normal ? can I turn it off?

هل كانت مفيدة؟

المحلول

Yes, it's normal. WPF buttons always do a subtle color cycling effect when they have input focus, and that's the case for your button since it's the only control on the form. It's not very subtle in your example because Red is a long way from the other blueish color the button cycles through -- delete that Background assignment to see the effect as intended. Try adding another ElementHost with another WPF Button to the same form, and you'll see that only the focused button does the color cycling.

As for how to turn it off... I don't know but I'm afraid it's not easy. I don't see any simple property on Button that would change this effect. Such effects are generally achieved by WPF style templates which is a subject that makes grown men cry. You can find the MSDN overview below, but note that this assumes you're working within WPF and XAML, not within Windows Forms:

http://msdn.microsoft.com/en-us/library/bb613570.aspx

My guess is that you would have to associate a changed Focused style with your button that won't do the color cycling, or else discover the resource name of the second brush (other than Background) that the Focused style cycles to, and set that resource to the same color as Background.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top