【问题标题】:WPF combobox transparent background not working with Windows 10WPF组合框透明背景不适用于Windows 10
【发布时间】:2017-06-05 07:59:07
【问题描述】:

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

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中不透明。

我已经通过 xaml 看到了一些解决方案,但不能仅在代码后面应用它们。 谢谢

【问题讨论】:

    标签: wpf combobox background windows-8 windows-10


    【解决方案1】:

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

    将默认模板复制到 XAML 标记后,您可以将 ToggleButton 样式中“templateRoot”边框元素的 Background 属性设置为 {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:

    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 属性:

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

    【讨论】:

    • 谢谢你,但正如我告诉你的,我不能使用 xaml,因为我正在构建一个 appdomain dll。简而言之,如何仅通过代码来做同样的事情?
    • 查看我编辑的答案。不过,我仍然会向您推荐这种 XAML 方式。
    • 很抱歉接受晚了。感谢您的解决方案和建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2015-06-27
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多