Bill Software Engineer
Bill Software Engineer

Reputation: 7782

In WPF, how do I apply a ControlTemplate to all Textbox

So I am new to WPF and I followed online tutorials to create a controlTemplate for Textbox, I could apply it by manually setting the style but I would like to it automatically to all textbox.

Now I read online for styles, you can simply just remove the Key definition and it will be universal, but I think that only apply to Style and not ControlTemplate, because when I do that, it throw an error saying "All objects added to an IDictionary must have a Key attribute or some other type of key association with them"

What do I do?

<ControlTemplate  TargetType="{x:Type TextBoxBase}">

Upvotes: 0

Views: 351

Answers (2)

Avinash patil
Avinash patil

Reputation: 1799

i am agree with the way of style. but there is another way, use UserControl with the text box. set all properties to the textbox as need. and simply use any where you want!

Upvotes: 2

Rohit Vats
Rohit Vats

Reputation: 81233

Create default style like you mentioned and set default template inside it so that it gets applied to all textBox. Make sure you place this under App.Resources so that it gets applied to textBoxes in all windows. If you want that to be applied on specific window place this under Window.Resources.

        <Style TargetType="TextBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                     ...
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Upvotes: 2

Related Questions