Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF altering property on just PART_EditableTextBox of a combBox

I would like to make a change to the PART_EditableTextBox of the default wpf combobox (change the background for example).

I tried adding a style like this:

<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="ComboBox">
        <TextBox x:Name="PART_EditableTextBox" Background="AntiqueWhite"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

But that causes the rest of the default template to be ignored.

Is there a way I can just override a specific property of the PART_EditableTextBox or do I have to copy over the entire control template and make my change in there?

like image 911
Flack Avatar asked Aug 17 '12 18:08

Flack


2 Answers

Some different ways to change the look of the Control...

Copying a Controls Template, Editing it and Using it in a Style

When copying and modify the Template of a control...you have to bear in mind 1 thing...themes.

The control might have completely different Template designs depending on the theme (i.e. different chrome)...and so your problem is...which template do you choose to copy, modify and then use on your control.

No matter, which one you choose...you have a problem...when someone is running Windows in a theme that is different to the theme from which you copied the Template from...well that control will look wrong/out of place.

To see how different Templates can look like in different themes...use ShowMeTheTemplate:

  • http://www.sellsbrothers.com/posts/details/2091

So to do it properly, you would have to copy and modify the template for each theme (Classic, Luna, Aero, Royale, etc) and do the necessary steps to get your different themed template to get loaded when the theme changes...so that your control is "theme aware".

  • When overriding WPF templates do I have to override each theme's template separately?

  • http://windowsclient.net/blogs/nidonocu/archive/2008/02/16/wpf-themes-and-control-libraries.aspx

  • http://blogs.windowsclient.net/nidonocu/archive/2008/03/03/wpf-themes-and-control-libraries-part-2.aspx

Modifying the Visual tree After Template has been applied at runtime

If you were the author of the control or you create a derived version of a control...then you can wait till the template is applied and then in OnApplyTemplate...you can then hunt for the "Part" in the visual tree, and then modify the visual tree/changes attributes at runtime (i.e. you could change the Background of PART_EditableTextBox).

However, this doesn't work if you are relying on implicit styles (as you are), or don't want to or can't replace all your controls with the derived version in your XAML.


Obtaining a Copy of the ControlTemplate at runtime, and modifying the "Part"

There is another possibility....obtaining the ControlTemplate for a control at runtime...(which will be for the current theme set at the time)....modifying it, then setting it onto the Control.

The beauty of this is if there are new themes in the system for which you didn't have prior knowledge of their names (and thus didn't design a Template for it), then instead of your control Template being picked up from Generic theme (and thus being out of place)....you have a better chance for the look to fit in better with the new theme. But it is a bit of a hack.

  • https://siderite.dev/blog/cloning-wpf-controltemplate.html

  • Define a WPF ControlTemplate at runtime

like image 123
CSmith Avatar answered Nov 15 '22 07:11

CSmith


Using 'BasedOn' will only overwrite the properties you specify in your new style.

However, in your case, the property you are overwriting is the Template. This is the entire template for the combobox, because that is the property you are trying to modify in your style.

To overwrite just part of it, you have to copy over the entire control template and make your change there; just like you thought.

If you don't have Expression Blend to retrieve the entire control template, you can find them on MSDN.

like image 26
Stewbob Avatar answered Nov 15 '22 09:11

Stewbob