I'm trying to dynamically change the background color of an editable ComboBox
at runtime, using code. In particular, I want to change the background of the editable TextBox
that is part of the ComboBox
.
There are several answers about this on SO, like this one:
WPF change the background color of an edittable combobox in code
however, the problem is that they're all based on XAML
and editing default templates. I don't want to do that, I'm searching for a generic solution that works with just code.
Is it possible? I tried the solution that seems obvious:
TextBox textBox = (TextBox)comboBox.Template.FindName("PART_EditableTextBox", comboBox);
textBox.Background = Brushes.Yellow;
But this does absolutely nothing. What am I missing?
This is how you can do it
<ComboBox Loaded="MyCombo_OnLoaded" x:Name="myCombo" IsEditable="True"></ComboBox>
private void MyCombo_OnLoaded(object sender, RoutedEventArgs e)
{
var textbox = (TextBox)myCombo.Template.FindName("PART_EditableTextBox", myCombo);
if (textbox!= null)
{
var parent = (Border)textbox.Parent;
parent.Background = Brushes.Yellow;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments