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

发布于 2024-12-20 18:46:48 字数 530 浏览 2 评论 0原文

我正在创建一个 WPF“按钮”控件并使其成为 ElementHost 控件的子控件。

按钮的背景设置为红色。

当我运行我的项目时,按钮会改变颜色,并且似乎每隔几秒钟循环一次,从红色变为浅蓝色,然后再返回......直到表单失去焦点。

如果我将鼠标悬停在按钮上,它会变成蓝色,然后当我移开按钮时,它会再次开始这种颜色循环..我的代码很简单......

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;
    }
}

这正常吗?我可以把它关掉吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2024-12-27 18:46:48

是的,这很正常。当 WPF 按钮具有输入焦点时,它们总是会产生微妙的颜色循环效果,按钮就是这种情况,因为它是表单上的唯一控件。在您的示例中,这并不是很微妙,因为红色与按钮循环经过的其他蓝色相距甚远 - 删除该背景分配以查看预期的效果。尝试将另一个 ElementHost 和另一个 WPF 按钮添加到同一窗体中,您将看到只有焦点按钮执行颜色循环。

至于如何关闭它……我不知道,但恐怕并不容易。我没有在 Button 上看到任何可以改变此效果的简单属性。这样的效果一般都是通过WPF风格的模板来实现的,这是一个让成年男子落泪的话题。您可以在下面找到 MSDN 概述,但请注意,这假设您在 WPF 和 XAML 中工作,而不是在 Windows 窗体中:

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

我的猜测是,您必须将更改后的 Focused 样式与您的按钮不会执行颜色循环,或者发现聚焦样式循环到的第二个画笔(背景除外)的资源名称,并将该资源设置为与背景相同的颜色。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文