使用 ScrollView 创建用户控件
Create a usercontrol with a ScrollView
我有一个 WPF 项目。
在这个项目中,我有一个 UserControl 和一个包含各种元素的 StackPanel。它位于网格中的一个单元格内。当我调整 Windwo 的大小时,Cell 变小以适合 Stackpanel,我希望滚动视图接管。
我尝试将 TheUserControl 放在 a 中,但这似乎只适用于设置大小。我需要它来适应动态单元格大小。我在网上找到的所有 "Solutions" 对于这样一个简单而常见的问题都是不必要的困难解决方法。所以我很确定有一种简单的方法可以实现这种行为。
伪代码
用户控件:
<UserControl x:class=x.TheUserControl>
<StackPanel>
<Label Content="Label 01 />
<Label Content="Label 02 />
<Label Content="Label 03 />
.
.
.
<Label Content="Label n />
</StackPanel>
</UserControl>
Window:
<Window x:Class="x.MainWindow>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Content="Header" />
<ScrollView Grid.Row="1">
<x:TheUserControl />
</ScrollView>
</Window>
我很确定当我将 StackPAnel 直接放入 ScrollView 时,ScrollView 工作得很好,那么为什么中间有一个 UserControl 会如此复杂?
我完全没有意识到 ScrollView 中的一些明显行为,如果有人能向我展示更好的方法或解释为什么会这样,我将非常高兴。
请尝试在您的用户控件中使用 ScrollViewer,并使用 HorizontalScrollBarVisibility 和 VerticalScrollBarVisibility:
用户控件:
<UserControl x:Class="WpfApp1.TheUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical" Background="LightGray">
<Label Content="Label 01" />
<Label Content="Label 02" />
<Label Content="Label 03" />
<Label Content="Label n" />
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
主窗口:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" >
<Grid Background="Aqua">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Content="Header" Grid.Row="0" />
<local:TheUserControl Grid.Row="1" />
</Grid>
</Window>
结果,如果您调整 window 的大小:
我有一个 WPF 项目。
在这个项目中,我有一个 UserControl 和一个包含各种元素的 StackPanel。它位于网格中的一个单元格内。当我调整 Windwo 的大小时,Cell 变小以适合 Stackpanel,我希望滚动视图接管。
我尝试将 TheUserControl 放在 a 中,但这似乎只适用于设置大小。我需要它来适应动态单元格大小。我在网上找到的所有 "Solutions" 对于这样一个简单而常见的问题都是不必要的困难解决方法。所以我很确定有一种简单的方法可以实现这种行为。
伪代码
用户控件:
<UserControl x:class=x.TheUserControl>
<StackPanel>
<Label Content="Label 01 />
<Label Content="Label 02 />
<Label Content="Label 03 />
.
.
.
<Label Content="Label n />
</StackPanel>
</UserControl>
Window:
<Window x:Class="x.MainWindow>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Content="Header" />
<ScrollView Grid.Row="1">
<x:TheUserControl />
</ScrollView>
</Window>
我很确定当我将 StackPAnel 直接放入 ScrollView 时,ScrollView 工作得很好,那么为什么中间有一个 UserControl 会如此复杂?
我完全没有意识到 ScrollView 中的一些明显行为,如果有人能向我展示更好的方法或解释为什么会这样,我将非常高兴。
请尝试在您的用户控件中使用 ScrollViewer,并使用 HorizontalScrollBarVisibility 和 VerticalScrollBarVisibility:
用户控件:
<UserControl x:Class="WpfApp1.TheUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical" Background="LightGray">
<Label Content="Label 01" />
<Label Content="Label 02" />
<Label Content="Label 03" />
<Label Content="Label n" />
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
主窗口:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" >
<Grid Background="Aqua">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Content="Header" Grid.Row="0" />
<local:TheUserControl Grid.Row="1" />
</Grid>
</Window>
结果,如果您调整 window 的大小: