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

时间:2019-07-09 05:47:23

标签: 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>

我要禁用以下行为:

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

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

我想禁用此自动滚动行为。有可能吗?

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

First textbox click After click in padding area

2 个答案:

答案 0 :(得分:1)

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

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>

C#:

private void StackPanel_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
  e.Handled = true;
}

答案 1 :(得分:0)

如果我了解您对问题的描述,可以使用:

<ScrollViewer HorizontalScrollBarVisibility="Hidden"  >...

或者也许Grid更适合您:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <TextBox 
    Grid.Column="0"
    Grid.ColumnSpan="2"
    TextWrapping="Wrap"
    AcceptsReturn="True"
    VerticalScrollBarVisibility="Auto"
    Height="100"
    Margin="0,5,0,5"
    Padding="20">Scrolling is enabled when it is necessary. 
            Resize the window, making it larger and smaller.</TextBox>
    <Label 
      Content="aswkognweklng"
      Grid.Column="0"
      Grid.Row="1">         
    </Label>
    <TextBox 
      TextWrapping="Wrap"
      AcceptsReturn="True"
      Height="100"
      VerticalScrollBarVisibility="Auto"
      Margin="0,5,0,5"
      Grid.Column="1"
      Grid.Row="1"
      Padding="20">Scrolling is enabled when it is necessary. 
            Resize the window, making it larger and smaller.</TextBox>
</Grid>