0
votes

i've got a ScrollViewer (name="master"), inside a StackPanel with some example Borders or rectangles and also a TextBox which have more Text than it's displayed, so the TextBox is scrollable. When you scroll the TextBox and came to the top oder bottom border the ScrollViewer which is in the TextBox Template (ScrollViewer x:Name="PART_ContentHost") routed the scrolling to the main ScrollViewer outside ("master").

is it possible to edit the TextBox Template to elimate this behavior?

One way is the combination with another ScrollViewer in the Template, this works fine, but it#s not more possible to see the selection so it's no solution.

Have you any other ideas?

<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
        <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                        <ScrollViewer x:Name="PART_ContentHost" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


<ScrollViewer Name="master" Height="71" Width="164" HorizontalAlignment="Right" Margin="0,0,38.666,28.833" VerticalAlignment="Bottom">
                    <StackPanel Orientation="Vertical" Width="121">
                        <Rectangle Fill="red" Height="45" Stroke="Black"/>
                        <Rectangle Fill="red" Height="45" Stroke="Black"/>
                        <Rectangle Fill="red" Height="45" Stroke="Black"/>
                        <Rectangle Fill="red" Height="45" Stroke="Black"/>
                        <TextBox Text="TextBox sydxr gs dgh drz h we rths dretghe dtrzuj hwesrtgh
     bdnftzh srdztj ser tghbed5rsetzhnrd hserdfgcjmnjs egrfhfn dshgrdxthgj" TextWrapping="Wrap" Height="67" Style="{StaticResource detail_text}" />
                        <Rectangle Fill="red" Height="45" Stroke="Black"/>
                        <Rectangle Fill="red" Height="45" Stroke="Black"/>
                    </StackPanel>
            </ScrollViewer>

Thanks a lot.

1

1 Answers

0
votes

My suggestion is to capture the PreviewMouseWheelEvent on the textbox itself:

<TextBox Text="TextBox sydxr gs dgh drz h we rths dretghe dtrzuj hwesrtgh
 bdnftzh srdztj ser tghbed5rsetzhnrd hserdfgcjmnjs egrfhfn dshgrdxthgj" TextWrapping="Wrap" Height="67" ScrollViewer.VerticalScrollBarVisibility="Auto" PreviewMouseWheel="TextBox_PreviewMouseWheel" />

Note that i included ScrollViewer.VerticalScrollBarVisibility="Auto" also.

Then evaluate if the scroll is being made when either on top or bottom and mark the event as handled if so:

private void TextBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    var textbox = sender as TextBox;
    var decorator = VisualTreeHelper.GetChild(textbox, 0) as Decorator;
    var scrollViewer = decorator.Child as ScrollViewer;

    if ((scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight && e.Delta < 0) ||
        (scrollViewer.VerticalOffset == 0 && e.Delta > 0))
    {
        e.Handled = true;
    }
}

No need to apply new templates this way, cheers!