Reputation: 221
How to change Combobox.Background?
Since Windows 8
ComboBox.Background=Brushes.Red
has no effect on ComboBox
Upvotes: 0
Views: 1814
Reputation: 160
I stumbled into the same problem and it brought me here.
Alex's answer gave me an idea, and after looking into the Windows 8 ComboBox's control template, I came to this solution:
private void ComboBox_Loaded(Object sender, RoutedEventArgs e)
{
var comboBox = sender as ComboBox;
var comboBoxTemplate = comboBox.Template;
var toggleButton = comboBoxTemplate.FindName("toggleButton", comboBox) as ToggleButton;
var toggleButtonTemplate = toggleButton.Template;
var border = toggleButtonTemplate.FindName("templateRoot", toggleButton) as Border;
border.Background = new SolidColorBrush(Colors.Red);
}
The plus side of this solution is that it's pretty straightforward, but it comes with a drawback: it seems to override all states, so there's little visual feedback on mouse over and such. I will edit my answer if I come up with a better idea.
Just make sure to add some error checking, as this can, and probably will, fail as soon as the code is executed on a system other than Windows 8.x, or if Microsoft ever attempts to fix the issue in question and modifies the template.
Upvotes: 2
Reputation: 3754
I know it's late, but I have found some way to fix it. Just use corrected ComboBox.
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
namespace Utils {
class ComboBoxWin8 : ComboBox {
public ComboBoxWin8() {
Loaded += ComboBoxWin8_Loaded;
}
void ComboBoxWin8_Loaded(object sender, RoutedEventArgs e) {
ControlTemplate ct = Template;
Border border = ct.FindName("Border", this) as Border;
// if Windows8
if (border != null) {
border.Background = Background;
// In the case of bound property
BindingExpression be = GetBindingExpression(ComboBoxWin8.BackgroundProperty);
if (be != null) {
border.SetBinding(Border.BackgroundProperty, be.ParentBindingBase);
}
}
}
}
}
Upvotes: 1