简体   繁体   English

托管在ElementHost控件中的WPF控件会更改颜色

[英]WPF control hosted in an ElementHost control changes colour

I'm creating a WPF 'Button' control and making it the child of my ElementHost control. 我正在创建WPF“按钮”控件,并将其作为ElementHost控件的子级。

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. WPF按钮在具有输入焦点时始终会产生微妙的颜色循环效果,而按钮正是这种情况,因为它是窗体上的唯一控件。 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. 在您的示例中,它不是很微妙,因为红色与按钮循环经过的其他蓝色颜色之间有很长的距离-删除该Background分配即可看到预期的效果。 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. 尝试将另一个带有另一个WPF按钮的ElementHost添加到相同的表单中,您会看到只有焦点按钮会进行颜色循环。

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. 我在Button上看不到任何简单的属性可以改变这种效果。 Such effects are generally achieved by WPF style templates which is a subject that makes grown men cry. 这种效果通常通过WPF样式模板来实现,而WPF样式模板是使成年男子哭泣的主题。 You can find the MSDN overview below, but note that this assumes you're working within WPF and XAML, not within Windows Forms: 您可以在下面找到MSDN概述,但是请注意,这是假设您在WPF和XAML而不是Windows Forms中工作:

http://msdn.microsoft.com/en-us/library/bb613570.aspx 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. 我的猜测是,您将不得不将更改的Focused样式与不会进行颜色循环的按钮相关联,否则将发现Focused样式循环到的第二个笔刷(背景除外)的资源名称,并进行设置资源与背景颜色相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM