Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollViewer control template breaks textboxes

I copied ScrollViewer template from MSDN (link) and it breaks textbox behavior. Scroll now doesn't follow selection when I select text using mouse. Why? How to fix it?

Here's my code

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">
  <Window.Resources>
    <Style TargetType="{x:Type ScrollViewer}">
      <Setter Property="OverridesDefaultStyle"
              Value="True" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ScrollViewer}">
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
              </Grid.RowDefinitions>
              <Border Grid.Column="1"
                      BorderThickness="0,1,1,1">
                <Border.BorderBrush>
                  <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                </Border.BorderBrush>
                <ScrollContentPresenter />
              </Border>
              <ScrollBar x:Name="PART_VerticalScrollBar"
                         Value="{TemplateBinding VerticalOffset}"
                         Maximum="{TemplateBinding ScrollableHeight}"
                         ViewportSize="{TemplateBinding ViewportHeight}"
                         Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
              <ScrollBar x:Name="PART_HorizontalScrollBar"
                         Orientation="Horizontal"
                         Grid.Row="1"
                         Grid.Column="1"
                         Value="{TemplateBinding HorizontalOffset}"
                         Maximum="{TemplateBinding ScrollableWidth}"
                         ViewportSize="{TemplateBinding ViewportWidth}"
                         Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
  <StackPanel>
    <TextBox Margin="20"
             Width="100"
             Text="sdjkfhsdjkfhjkdshxcmvnm,xcnv,mxcnv, mxcnv,mxcngjklsdjkfh jkdfghjkfhgjkdfhgkjdfghkjdfhgbkjcvhbkcvjbh" />
  </StackPanel>
</Window>

I use .NET 4

like image 966
Poma Avatar asked Jul 15 '26 15:07

Poma


1 Answers

I had the same problem, I finally solved by adding CanContenScroll="True" to ScrollContentPresenter.

<ScrollContentPresenter Grid.Column="0" CanContentScroll="True"/>

and if you want to make it more flexible just bind it to ancestor value:

 <ScrollContentPresenter Grid.Column="0" 
                         CanContentScroll="{TemplateBinding CanContentScroll}" />

hope it helps

like image 122
Juan M. Vergara Avatar answered Jul 17 '26 15:07

Juan M. Vergara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!