Home > OS >  Why cant I write and Display text in my TextBox what is made with an Style and an ControlTemplate?
Why cant I write and Display text in my TextBox what is made with an Style and an ControlTemplate?

Time:09-16

so I got an TextBox and i made the Style in my Ressource Dictionary like that:

 <Style x:Key="TextBoxTemplateBrowser" TargetType="{x:Type TextBox}">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Background="White" CornerRadius="0 0 5 5" BorderBrush="Black" BorderThickness="1">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And my TextBox himself like that:

 <TextBox TextWrapping="Wrap" x:Name="tb" Style="{DynamicResource TextBoxTemplateBrowser}"
                 Text="{Binding Inhalt, RelativeSource={RelativeSource AncestorType=local:ArtikelBezPanel}, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                 TextChanged="TextBox_TextChanged" 
                 Height="{Binding Height, RelativeSource={RelativeSource AncestorType=local:ArtikelBezPanel}, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" AcceptsReturn="True" BorderBrush="Black" AcceptsTab="True" VerticalAlignment="Top" BorderThickness="1">
        </TextBox>

The Problem what I got now is that, when i try to write in that TextBox I can write anything and there cant be Text displayed what I declare before.

CodePudding user response:

When templating TextBox you need the PART_ContentHost:

eg:

<ScrollViewer Margin="0" x:Name="PART_ContentHost" />

See DOCS for more info!

Replace

<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 

with

<ScrollViewer Margin="0" x:Name="PART_ContentHost" HorizontalAlignment="Center" VerticalAlignment="Center"/>

and it should work!

  • Related