C# stackpanel的水平滚动显示不';行不通
我试图创建一个可滚动的水平堆叠面板,但我没有很好地成功 目前,我的StackPanel具有C# stackpanel的水平滚动显示不';行不通,c#,.net,wpf,scroll,stackpanel,C#,.net,Wpf,Scroll,Stackpanel,我试图创建一个可滚动的水平堆叠面板,但我没有很好地成功 目前,我的StackPanel具有自动宽度(问题可能在这里),其中包含一些项目,如网格 现在,如果我的所有网格在StackPanel中都不可见(宽度太短),我将无法滚动。 我已经尝试将StackPanel放入ScrollViewer中,但没有成功 我也不工作 我怎样才能解决这个问题 编辑以下是我的代码: 您应该像这样将StackPanel放入ScrollViewer: <ScrollViewer Height="85" Vertica
自动
宽度(问题可能在这里),其中包含一些项目,如网格
现在,如果我的所有网格在StackPanel中都不可见(宽度太短),我将无法滚动。
我已经尝试将StackPanel放入ScrollViewer
中,但没有成功
我也不工作
我怎样才能解决这个问题
编辑以下是我的代码:
您应该像这样将StackPanel放入ScrollViewer:
<ScrollViewer Height="85" VerticalAlignment="Bottom" Margin="0,0,200,15" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto">
<StackPanel x:Name="Film" Orientation="Horizontal" >
<StackPanel.Background>
<SolidColorBrush Color="Black"/>
</StackPanel.Background>
<Grid Width="100" Background="Red"/>
<Grid Width="100" Background="#FFFF0051"/>
<Grid Width="100" Background="#FFB900FF"/>
<Grid Width="100" Background="#FF002EFF"/>
<Grid Width="100" Background="#FF00FFDC"/>
<Grid Width="100" Background="#FF51FF00"/>
<Grid Width="100" Background="Red"/>
</StackPanel>
</ScrollViewer>
目前我有一个自动宽度的stackpanel(问题可能在这里),其中包含一些项目,如网格
这是你的问题。如果StackPanel的“方向”属性设置为“水平”,StackPanel将使用无限水平空间测量其子级;如果StackPanel的“方向”属性设置为“垂直”,StackPanel将使用无限垂直空间测量其子级。因此,您必须为StackPanel本身或ScrollViewer指定一个显式宽度,才能使其工作
或者,您可以将ScrollViewer放在测量其子级的面板中,例如网格(但不是StackPanel)。例如:
<Window x:Class="WpfApplication1.Window1"
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:WpfApplication1"
mc:Ignorable="d"
Title="Window" Height="300" Width="300">
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True">
<StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition">
<StackPanel.Background>
<SolidColorBrush Color="Yellow"/>
</StackPanel.Background>
<Grid Width="100" Background="Red"/>
<Grid Width="100" Background="#FFFF0051"/>
<Grid Width="100" Background="#FFB900FF"/>
<Grid Width="100" Background="#FF002EFF"/>
<Grid Width="100" Background="#FF00FFDC"/>
<Grid Width="100" Background="#FF51FF00"/>
<Grid Width="100" Background="Red"/>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
但这并不是因为StackPanel被认为具有无限宽度:
<StackPanel>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True">
<StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition">
<StackPanel.Background>
<SolidColorBrush Color="Yellow"/>
</StackPanel.Background>
<Grid Width="100" Background="Red"/>
<Grid Width="100" Background="#FFFF0051"/>
<Grid Width="100" Background="#FFB900FF"/>
<Grid Width="100" Background="#FF002EFF"/>
<Grid Width="100" Background="#FF00FFDC"/>
<Grid Width="100" Background="#FF51FF00"/>
<Grid Width="100" Background="Red"/>
</StackPanel>
</ScrollViewer>
</StackPanel>
将ScrollViewer放入stackpanel是个坏主意。stackpanel的前身是什么?你不能使用自动宽度的滚动视图检查线程,它将帮助你它无法工作,除非你真的把整个东西放在滚动查看器中。谢谢你,我用滚动查看器尝试了其他东西,现在它工作了(我更新了主帖子中的代码),但是有可能使滚动平滑吗?目前,每个网格都是“一步一步”的。编辑Ok my bad,我只需将
CanContentScroll
切换到False