Stylzs05
Stylzs05

Reputation: 521

XAML - Text is not showing in my textbox after I styled it

So, I styled my TextBoxes in an app that I'm working on, and all of a sudden I can't see any text that I've bound to my TextBoxes. I feel like I'm missing some kind of ContentPresenter. Well anyway, here is the styling.

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Height" Value="26"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}">
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Value="#FF2F2F2F" Property="Background"/>
                        <Setter Value="White" Property="Foreground"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And here is how my TextBoxes are set up

<TextBox Grid.Row="2" Grid.Column="5" BorderThickness="1" Text="{Binding VariableName}">

Any thoughts?

Upvotes: 2

Views: 5038

Answers (1)

user7116
user7116

Reputation: 64068

Your hunch is right, you need to have a template part with the name PART_ContentHost inside your ControlTemplate:

<ControlTemplate TargetType="TextBox">
    <Border BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}">
        <ScrollViewer Margin="0"
                    x:Name="PART_ContentHost" />
    <!-- ... -->

This is because the ControlTemplate for a TextBox requires a part with the name PART_ContentHost. You can view the ControlTemplate examples for the built-in controls to find out what named template parts are required and what each part must be able to do in order to retain normal functionality.

Upvotes: 6

Related Questions