我必须通过后面的代码定义一个组合框:
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看到了一些解决方案但是只能在代码后面应用它们。 感谢名单
答案 0 :(得分:3)
您无法简单地设置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标记后,您可以设置&#34; templateRoot&#34;的背景属性。 ToggleButton样式中的边框元素为{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;
}
};