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

查看:37
本文介绍了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

推荐答案

在 Windows 8 和 10 上,您不能简单地设置 ComboBox 的背景属性来更改其背景.您需要按照建议定义自定义控件模板在这里:https://blog.magnusmontin.net/2014/04/30/changed-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">
...

然后,您必须将自定义样式应用到您以编程方式创建的组合框:

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天全站免登陆