WPF 组合框透明背景不适用于 Windows 10
WPF combobox transparent background not working with Windows 10
我必须通过隐藏代码定义一个组合框:
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 看到了一些解决方案,但无法仅在代码隐藏中应用它们。
谢谢
您不能简单地设置 ComboBox 的背景 属性 来更改其在 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" 边框元素的背景 属性 设置为 {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 元素并设置其背景 属性:
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;
}
};
我必须通过隐藏代码定义一个组合框:
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 看到了一些解决方案,但无法仅在代码隐藏中应用它们。 谢谢
您不能简单地设置 ComboBox 的背景 属性 来更改其在 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" 边框元素的背景 属性 设置为 {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 元素并设置其背景 属性:
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;
}
};