如何禁用 Scrollviewer 上子控件的自动滚动

Der*_*Ape 1 c# wpf scrollviewer

我有以下相当简单的代码

  <Window ... Width=400 Height=400>
    <ScrollViewer HorizontalScrollBarVisibility="Auto"  >
        <StackPanel VerticalAlignment="Top"
                    HorizontalAlignment="Left">
            <TextBox TextWrapping="Wrap"
                     Margin="0,5,0,5"
                     Width="500"
                     Padding="20">Scrolling is enabled when it is necessary. 
                Resize the window, making it larger and smaller.</TextBox>
            <StackPanel Orientation="Horizontal">
                <Label Content="aswkognweklng"></Label>
                <TextBox TextWrapping="Wrap"
                         Margin="0,5,0,5"
                         Width="500"
                         Padding="20">Scrolling is enabled when it is necessary. 
                Resize the window, making it larger and smaller.</TextBox>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
  </Window>
Run Code Online (Sandbox Code Playgroud)

我想禁用以下行为:

  • 滚动条可见
  • 单击第一个文本框中的文本
  • 单击第二个文本框中的填充区域

=> 滚动查看器将移动滚动条,使文本框左边框与可见窗口边框对齐

我想禁用这种自动滚动行为。那可能吗?

用户交互的默认滚动行为应该仍然有效。因此,当用户与滚动条交互时,它应该正常滚动内容。

首先单击文本框 单击填充区域后

Vis*_*abu 6

对的,这是可能的。ScrollViewer您只需要处理冒泡到第二个的 RequestBringIntoView 事件StackPanel。只需将其标记为已处理即可。

XAML:

<ScrollViewer HorizontalScrollBarVisibility="Auto"  >
    <StackPanel VerticalAlignment="Top"
                HorizontalAlignment="Left">
        <TextBox TextWrapping="Wrap"
                 Margin="0,5,0,5"
                 Width="500"
                 Padding="20">Scrolling is enabled when it is necessary. 
            Resize the window, making it larger and smaller.</TextBox>
        <StackPanel Orientation="Horizontal" RequestBringIntoView="StackPanel_RequestBringIntoView">
            <Label Content="aswkognweklng"></Label>
            <TextBox TextWrapping="Wrap"
                     Margin="0,5,0,5"
                     Width="500"
                     Padding="20">Scrolling is enabled when it is necessary. 
            Resize the window, making it larger and smaller.</TextBox>
        </StackPanel>
    </StackPanel>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

C#:

private void StackPanel_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
  e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)