ScrollViewer 中的用户控件不会在 WPF 中滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15135354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:47:27  来源:igfitidea点击:

User control inside ScrollViewer doesn't scroll in WPF

wpfxamluser-controlsscrollviewer

提问by Onur

I have the following code (abbreviated) in my main window:

我的主窗口中有以下代码(缩写):

Although I set both scroll bar visibilities and CanContentScrollproperties it doesn't scroll. I assume it has to do with my user control.

尽管我设置了滚动条可见性和CanContentScroll属性,但它不会滚动。我认为这与我的用户控制有关。

<Window>
   <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
       <TabControl  Grid.Column="0" Grid.Row="0"  HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
          <TabItem Header="TEST">
            <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
            <my:MY_USER_CONTROL  x:Name="myUserControl"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  ScrollViewer.CanContentScroll="True" />
            </ScrollViewer>
        </TabItem>
      </TabControl>
      <Button Grid.Column="0"  Grid.Row="2" >a button</Button>
      <WrapPanel Grid.Column="0" Grid.Row="3" >
      </WrapPanel>
    </Grid>
  </Window>

Abbreviated structure of my user control:

我的用户控件的缩写结构:

<UserControl>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="183*" />
            <ColumnDefinition Width="117*" />
        </Grid.ColumnDefinitions>
        <TreeView ItemsSource="{Binding Children}" Grid.ColumnSpan="2">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">

                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />

                    <Setter Property="FontWeight" Value="Normal" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}" >
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.Children>
                            <TextBlock Background="LightGray"  Padding="2" Margin="2" Grid.Row="0" Grid.Column="0" Text="{Binding Name}" />
                            <TextBlock Padding="2" Margin="2" Grid.Row="0" Grid.Column="1" Text="{Binding Content}" />
                        </Grid.Children>
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</UserControl>

回答by hiHyman

You need to set it like this. I changed the RowDefinition for Row0 to Height="*"So it will use as much space it can. Then changed place between the ScrollViewer and the TabControl. So the TabControl is a content of the ScrollViewer.

你需要这样设置。我将 Row0 的 RowDefinition 更改为Height="*"因此它将尽可能多地使用空间。然后在 ScrollViewer 和 TabControl 之间改变了位置。所以TabControl 是ScrollViewer 的一个内容。

<Window>
 <Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <ScrollViewer HorizontalScrollBarVisibility="Auto"
              VerticalScrollBarVisibility="Auto"
              Grid.Column="0" Grid.Row="0"> 
   <TabControl  HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Stretch"
               Height="Auto" Width="Auto">
      <TabItem Header="TEST">
        <my:MY_USER_CONTROL x:Name="myUserControl"  
                            HorizontalAlignment="Stretch"              
                            VerticalAlignment="Stretch" 
                            ScrollViewer.CanContentScroll="True" />          
     </TabItem>
  </TabControl>
 </ScrollViewer>
 <Button Grid.Column="0"  Grid.Row="2" >a button</Button>
 <WrapPanel Grid.Column="0" Grid.Row="3" >
 </WrapPanel>
</Grid>

回答by Joachim Kerschbaumer

When you set CanContentScrollto True, the ScrollViewer assumes that your content implements IScrollInfo (which i guess doesn′t).

当您将CanContentScroll设置为True 时, ScrollViewer 假定您的内容实现 IScrollInfo (我猜没有)。

Try setting CanContentScrollon the ScrollViewer to false, this allows the content to use as much space as it wants and the ScrollViewer takes care of scrolling.

尝试将ScrollViewer上的CanContentScroll设置为 false,这允许内容使用尽可能多的空间,并且 ScrollViewer 负责滚动。

However, depending on the size, number of visuals etc. of your control, this might become a performance issue (e.g. no UI Virtualization when CanContentScrollis set to False).

但是,根据控件的大小、视觉效果等的数量,这可能会成为性能问题(例如,当CanContentScroll设置为False时没有 UI 虚拟化)。

回答by Andy

Looks like the content inside your scrollviewer is the same size as the viewer so there is nothing to scroll?

看起来您的滚动查看器中的内容与查看器的大小相同,因此没有可滚动的内容?

if you do something like

如果你做类似的事情

<ScrollViewer HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <Grid Background="Red">

    </Grid>
</ScrollViewer>

Then the grid is the same size as the scroll viewer and will never allow scrolling, if you set the height of the grid to more than the viewer can display you'll get a scroller.

然后网格与滚动查看器的大小相同,并且永远不允许滚动,如果您将网格的高度设置为超过查看器可以显示的高度,您将获得滚动条。

<ScrollViewer HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <Grid Background="Red" Height="500">

    </Grid>
</ScrollViewer>