Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 599907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:32:57+00:00 2026-05-13T16:32:57+00:00

I’ve built a UserControl that extends the functionality of the ComboBox in interesting and

  • 0

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:

My user control

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:32:58+00:00Added an answer on May 13, 2026 at 4:32 pm

    You have questions. I have answers.

    1- Why is the ScrollViewer’s Background binding behaving so strangely?

    When a TextBox is first measured, it instantiates its template. This creates the ScrollViewer. Once the template is applied, the TextBox checks to see if the ScrollViewer‘s Background property currently has a null value. If so, it overwrites it with Background.Transparent. Doing so disconnects your binding.

    This is why it works when you set it in the constructor but not later on: The TextBox saw the null value and overwrote it with Background.Transparent, breaking the binding.

    2- What is the ScrollViewer doing there anyway?

    TextBox is a Control that doesn’t actually handle any of the gory details of text presentation itself – if you browse the visual tree you’ll see that this is handled by another Visual with a name something like “TextView”. TextBox‘s main job is actually to present the border around the textbox and/or allow you to give it a totally new look.

    TextBox requires a template with an elemnent named PART_ContentHost which is either a ContentPresenter or a ScrollViewer. If it is a simple ContentPresenter, the internal “TextView” object is simply added to it. If it is a ScrollViewer, the TextBox also wires up some additional functionality such as scrolling text into view when it is focused.

    The ScrollViewer‘s purpose in life is to allow the text in the TextBox to scroll horizontally, and also scroll vertically for multiline text boxes.

    3- Why did Blend give me a different control template

    Blend loads the actual ControlTemplate XAML from the referenced assemblies, in this case PresentationFramework.dll and the associated theme dll for the current system theme. So it will load what is actually being used in the version of NET Framework you have installed. The XAML on the site you linked is just example code, and is not the actual NET Framework XAML.

    I added two more relevant questions:

    4- Why didn’t setting the TextBox’s Background property work?

    None of WPF’s Control subclasses actually implement their Background properties themselves. The Background DependencyProperty is just a named brush that a Control’s template can bind to if it likes. This is just as true for TextBox as for any other Control. The default TextBox template includes a “chrome” object that includes code to display the background, similar to what you might use a border for. Since ComboBox is already displaying its own “chrome”, it uses its own TextBox template that includes the ScrollViewer but not the surrounding chrome. That is why setting the Background property on the TextBox inside a ComboBox has no effect.

    5- How do I solve my problem and bind the background color of the TextBox inside the ComboBox

    If you’re ok with the white margin you can simply wrap the ScrollViewer inside in a <Border> and set the background on the <Border>. If not, you will have to move the desired background into the chrome supplied in the main ControlTemplate for the ComboBox.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.