Jishan
Jishan

Reputation: 1684

TextBox ScrollBar Not Showing After Customization

I designed my custom WPF TextBox as follows.

<TextBox VerticalAlignment="Bottom" Name="txtResult" Padding="8" FontFamily="Consolas" Margin="5" TextWrapping="Wrap">
                <TextBox.Template>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <TextBox Margin="4" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text}" BorderThickness="0" Background="{x:Null}" SnapsToDevicePixels="True"  ScrollViewer.VerticalScrollBarVisibility="Visible"/>
                            <Rectangle  Stroke="{StaticResource ResourceKey=detailMarkBrush}" StrokeThickness="0"/>
                        </Grid>
                    </ControlTemplate>
                </TextBox.Template>
            </TextBox>

But even after adding the ScrollViewer.VerticalScrollBarVisibility="Visible" or wrapping the TextBox in ScrollViewer, my scrollbars don't show. Any suggestions?

Thanks.

Upvotes: 0

Views: 57

Answers (1)

CptObvious
CptObvious

Reputation: 396

Your ControlTemplate's target already is a TextBox, so there should be no reason for the ControlTemplate to contain another TextBox. Therefore, it could be replaced by a ScrollViewer as a corresponding content host.
Based on that, your TextBox could look like this:

<TextBox VerticalAlignment="Bottom"
         Name="txtResult"
         Padding="8"
         FontFamily="Consolas"
         Margin="5"
         VerticalScrollBarVisibility="Visible"
         TextWrapping="Wrap">
    <TextBox.Template>
        <ControlTemplate TargetType="{x:Type TextBox}">
            <Grid>
                <ScrollViewer x:Name="PART_ContentHost"
                              Padding="{TemplateBinding Padding}"
                              BorderThickness="0" 
                              IsTabStop="False" 
                              Margin="4"
                              Background="{x:Null}"/>
                <Rectangle  Stroke="{StaticResource ResourceKey=detailMarkBrush}"
                            StrokeThickness="0"/>
            </Grid>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>

If you want to use this template on several TextBoxes, I'd recommend a separate style for that.

(Is there a reason that your rectangle has a StrokeThickness of 0?)

Upvotes: 1

Related Questions