c# - 如何在 ScrollViewer 中滚动水平 StackPanel?

标签 c# wpf xaml scrollviewer stackpanel

我是 wpf 新手,我需要帮助。我必须水平显示 n 个垂直元素(堆栈面板)。我使用水平堆栈面板将所有 n 个垂直堆栈面板放入其中,并将水平堆栈面板放入滚动查看器中。 这是下面的 xaml 代码:

`<Window x:Class="Results.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <telerik:RadButton x:Name="radButton" Content="Button" HorizontalAlignment="Left" Height="Auto" Margin="407,66,0,0" VerticalAlignment="Top" Width="Auto" Click="radButton_Click"/>
    <Grid>
        <ScrollViewer  Height="96" Margin="10,115,0,0"  Width="497" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
            <StackPanel x:Name="teststack" HorizontalAlignment="Left" Height="94" VerticalAlignment="Top" Width="497" Orientation="Horizontal"/>
        </ScrollViewer>
    </Grid>
</Grid>`

这是用于生成下面 n 个元素的 C# 代码(其中 col 是垂直的,teststack 是水平的):

for (int i = 0; i < arrowcount; i++)
{
    StackPanel col = new StackPanel();
    TextBox row = new TextBox();
    Image img = new Image();
    col.Children.Add(row);
    col.Children.Add(img);
    teststack.Children.Add(col);
}

this image是我不想要的结果。因为我显示了 20 个元素,但没有滚动条可以看到 12 个元素。我的理想是在元素下的该图像中可见水平滚动条。 如果我以错误的方式提出问题,请建议我进行编辑。

最佳答案

尝试不使用 ScrollViewerHeightWidth

<ScrollViewer  Margin="10,115,0,0" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
     <StackPanel x:Name="teststack" HorizontalAlignment="Left" Height="94" VerticalAlignment="Top" Width="497" Orientation="Horizontal"/>
</ScrollViewer>

关于c# - 如何在 ScrollViewer 中滚动水平 StackPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57684538/

相关文章:

c# - Newtonsoft Json反序列化: How to throw an error if/when the given json string has MORE properties than necessary?

c# - C# 属性的属性在 Getter 调用后发生变化

c# - Treeview ContainerFromItem 总是返回 null

wpf - 如何在 Blend 中将径向渐变的 X/Y 坐标数据绑定(bind)到鼠标光标的 X/Y 坐标?

xaml - 当我更改主题设置时,为什么我的 AdaptiveTrigger 会触发?

C# BitmapData 类 Scan0 和 Stride 属性含义

.net - 自定义 WPF DatePickerTextBox 模板

wpf - INotifyPropertyChanged、ObservableCollection、线程和 MVVM

c# - 嵌套用户控件的 WPF、MVVM 和事件处理

xaml - 应用程序栏 Windows Phone