Search code examples
c#wpf

WPF TextBox Styling - Disable scrolling?


it's my first question here so be gentle haha

I am using a WPF TextBox Element to enter some user information, in this example it's a username. My problem with this is that I am able to scroll within the TextBox which I don't want, as it just looks terrible even if it is just a bit.

So I am looking for an option to turn the scrolling off. As you can see I am using a ScrollViewer, as the documentation suggests but if there are any other options for styling the TextBox I am more then willing to try :D Thanks in advance.

<Style TargetType="{x:Type TextBox}"
       x:Key="textBox1">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border CornerRadius="16"
                        Background="{TemplateBinding Background}">
                    <ScrollViewer Margin="10,7.5"
                                  x:Name="PART_ContentHost"/>

                </Border>
            </ControlTemplate>

        </Setter.Value>
    </Setter>

=> This is the Style of the TextBox in a RessourceDic.

<StackPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">
        <Grid Height="250"/>
        

        <TextBlock x:Name="Error"
                   FontSize="14"
                   Foreground="DarkRed"
                   FontWeight="Bold"
                   Margin="5"
                   TextWrapping="Wrap"/>

        <TextBlock Text="Benutzername" Margin="15,0,0,10" FontSize="20"/>
        <TextBox Margin="0,0,20,0"
                 Background="#F2E3D5"
                 x:Name="Username"
                 Style="{StaticResource textBox1}"
                 Width="230"
                 Height="35"
                 FontSize="21">
           
        </TextBox>
        <TextBlock Text="Passwort" Margin="15,20,0,10" FontSize="20"/>
        <PasswordBox Margin="0,0,20,0"
                     Background="#F2E3D5"
                     x:Name="Password"
                     Style="{StaticResource passwordBox1}"
                     FontSize="21">

        </PasswordBox>

        <Button x:Name="LogIn" Height="40" Width="120" Style="{StaticResource LogInBTN}" Foreground="Black" Margin="0,30,0,0" Click="LogInBTN_Click">

            <Button.Content>
                <StackPanel Orientation="Horizontal">
                    <Image Source="images/user.png" Height="25" Width="25" VerticalAlignment="Center"/>
                    <TextBlock Text="Einloggen" Margin="10,2,0,0" VerticalAlignment="Center"/>
                </StackPanel>
            </Button.Content>

        </Button>

        <TextBlock Margin="0,20,0,0" Height="20" Width="250" Text="X - Passwort nicht korrekt" TextAlignment="Center" FontSize="16" Foreground="Red" Visibility="Hidden"/>
    </StackPanel>

=> This is the Stackpanel where I use the TextBox.


Solution

  • as workaround, without overriding ScrollViewer:

    <TextBox Height="30" Width="50">
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Grid>
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                    <Decorator x:Name="PART_ContentHost"/>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TextBox.Style>
    </TextBox>