WPF组合框透明背景不适用于Windows 10 [英] WPF combobox transparent background not working with Windows 10

查看:122
本文介绍了WPF组合框透明背景不适用于Windows 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过后面的代码定义一个组合框:

I have to define a combobox through code behind:

var cmbLogin = new ComboBox()
{
    Width = 200,
    Height = m_dFontSize + 10,
    FontSize = m_dFontSize,
    Margin = new Thickness(20),
    BorderBrush = new SolidColorBrush(m_ExeCfg.GetForeground()),
    HorizontalContentAlignment = HorizontalAlignment.Center,
    Background = Brushes.Transparent,<--------------HERE
    Foreground = new SolidColorBrush(m_ExeCfg.GetForeground()),
    Focusable = true,
};

因此背景在win7中变得透明,而在win10中却不透明.

so the background gets transparent in win7 but not in win10.

我已经通过xaml看到了一些解决方案,但是无法仅在后面的代码中应用它们. 谢谢

I have seen some solutions through xaml but could'nt apply them in code behind only. Thanx

推荐答案

您不能简单地设置ComboBox的Background属性以更改其在Windows 8和10上的背景.您将需要按照建议定义自定义控件模板此处: https://blog.magnusmontin.net/2014/04/30/changing-the-background-colour-of-a-combobox-in-wpf-on-windows-8/.

You can't simply set the Background property of the ComboBox to change its background on Windows 8 and 10. You will need to define a custom control template as suggested here: https://blog.magnusmontin.net/2014/04/30/changing-the-background-colour-of-a-combobox-in-wpf-on-windows-8/.

将默认模板复制到XAML标记后,可以将ToggleButton样式中"templateRoot" Border元素的Background属性设置为{TemplateBinding Background}

Once you have copied the default template into your XAML markup you could set the Background property of the "templateRoot" Border element in the ToggleButton style to {TemplateBinding Background}

<ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border x:Name="templateRoot" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
...

然后,您必须将自定义样式应用于以编程方式创建的ComboBox:

You will then have to apply the custom style to the ComboBox that you are creating programmatically:

var cmbLogin = new ComboBox()
{
    Width = 200,
    Height = m_dFontSize + 10,
    FontSize = m_dFontSize,
    Margin = new Thickness(20),
    BorderBrush = new SolidColorBrush(m_ExeCfg.GetForeground()),
    HorizontalContentAlignment = HorizontalAlignment.Center,
    Background = Brushes.Transparent,
    Foreground = new SolidColorBrush(m_ExeCfg.GetForeground()),
    Focusable = true,
    Style = Resources["ComboBoxStyle1"] as Style
};

如果您真的想要完全不使用任何XAML标记,则必须等到ComboBox加载完毕,然后在可视树中找到Border元素并设置其Background属性:

If you really, really want to this without using any XAML markup at all you will have to wait until the ComboBox has been loaded and then find the Border element in the visual tree and set its Background property:

var cmbLogin = new ComboBox()
{
    Width = 200,
    Height = m_dFontSize + 10,
    FontSize = m_dFontSize,
    Margin = new Thickness(20),
    BorderBrush = new SolidColorBrush(m_ExeCfg.GetForeground()),
    HorizontalContentAlignment = HorizontalAlignment.Center,
    Foreground = new SolidColorBrush(m_ExeCfg.GetForeground()),
    Focusable = true,
};

cmbLogin.Loaded += (ss, ee) => 
{
    var toggleButton = cmb.Template.FindName("toggleButton", cmbLogin) as System.Windows.Controls.Primitives.ToggleButton;
    if(toggleButton != null)
    {
        Border border = toggleButton.Template.FindName("templateRoot", toggleButton) as Border;
        if (border != null)
            border.Background = Brushes.Transparent;
    }
};

这篇关于WPF组合框透明背景不适用于Windows 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆