I've built a UserControl that extends the functionality of the ComboBox in interesting and useful ways. It looks like this when it's dropped down:
I've built a whole bunch of features into the control and they all work smoothly. This leads me to believe that I have something of a clue as to what I'm doing. You'd think it would be a trivial matter to have the UserControl's style set the editable TextBox's background brush. In fact, it appears to be freaking impossible. And I'm baffled.
The UserControl's XAML, extremely abbreviated (you'll thank me for this), looks like this:
<UserControl x:Class="MyApp.CodeLookupBox" x:Name="MainControl">
<UserControl.Resources>
<!-- tons of DataTemplates and Styles, most notably the style that
contains the control template for the ComboBox -->
<UserControl.Resources>
<ComboBox x:Name="ComboBox"
Margin="0"
Style="{DynamicResource ComboBoxStyle1}"
VerticalAlignment="Top"
ItemTemplate="{StaticResource GridViewStyleTemplate}"/>
</UserControl>
There's a lot of code-behind in this control, mostly dependency properties that I use for things like selecting the template that's used in the dropdown.
What's making me crazy is the editable text box. I want to be able to set its background brush from the user control's style - e.g., when I declare one of these user controls in my XAML, it uses a style like this:
<Style TargetType="{x:Type local:CodeLookupBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsRequired}" Value="True">
<Setter Property="EditableTextBoxBackground" Value="{StaticResource RequiredFieldBrush}"/>
</DataTrigger>
</Style.Triggers>
</Style>
I started out life simply setting the UserControl's Background, but that set the background behind the editable TextBox. The TextBox itself remained white.
Inside the template for the ComboBox, there's a style that controls that TextBox:
<Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="MinWidth" Value="0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<ScrollViewer
x:Name="PART_ContentHost"
Focusable="false"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And there is the TextBox (inside the ComboBox's control template) its bad self:
<TextBox
x:Name="PART_EditableTextBox"
Margin="{TemplateBinding Padding}"
Style="{StaticResource ComboBoxEditableTextBox}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"/>
Now, there's a definite element of Things Man Was Not Meant To Know about that ComboBoxEditableTextBox style. What is that ScrollViewer doing in there? I have no idea. I can tell you that if I comment out the part of the style that sets the TextBox's ControlTemplate, very bad things happen.
And I also know this: If I explicitly set the TextBox's Background brush as one of the style's setters, nothing happens. If I explicitly set the Background on the PART_EditableTextBox, nothing happens. (I can set its Foreground, or its FontFamily, and they work just fine.)
If I explicitly set the Background of that ScrollViewer to Green, though, voila, the TextBox turns green.
Okay then, so the TextBox is ignoring its own Background, and using the one from its control template. Actually, it's not, strictly speaking, using the one from the control template either. When I set the ScrollViewer's background, there's a decided margin around the edges of the color, instead of the color filling the TextBox entirely. But that margin is white, not the background color.
Unless I can figure out why the TextBox is ignoring its Background, I have to live with tweaking the ScrollViewer. So how do I get it to get the brush from the user control's EditableTextBoxBackground property? I've made this a dependency property that raises the PropertyChanged event properly when it changes. I'm binding to it in the mysterious ScrollViewer's XAML like this:
Background="{Binding ElementName=MainControl,
Path=EditableTextBoxBackground,
Converter={StaticResource DebuggingConverter}}"
I've set a breakpoint in my debugging converter. When the control is first drawn, it gets hit twice. The first time, the value of the brush is null. The second time, it's the right value. And if I set the property in my UserControl's constructor, it works.
So here's what I know: My UserControl's property is being set properly. The binding on the TextBox's style is correctly bound to the UserControl's property. The binding on the ScrollViewer in the TextBox's control template is bound to the right property. The property raises PropertyChanged with the right property name when it's changed, and the binding pushes the value out to the ScrollViewer Background property.
And nothing happens.
So I guess I have a three part question: 1) Why? 2) What the heck is that ScrollViewer doing in there in the first place? I have my suspicions, but it's one in the morning and it's getting hard for me to articulate them. 3) Why did Blend give me a different control template to work with than the much more comprehensible one found here?
Really, any help would be appreciated.