WPF 滚动 Uniformgrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17583278/
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 09:11:49  来源:igfitidea点击:

WPF Scroll a Uniformgrid

wpfxamlitemscontrolscrollvieweruniformgrid

提问by BlackCath

I need to display all the files located in a specific path. I have created a user control which contains textblocks for the details of the file (name, size, extension, etc), this control will be the child of the uniform grid.

我需要显示位于特定路径中的所有文件。我创建了一个用户控件,其中包含文件详细信息(名称、大小、扩展名等)的文本块,该控件将是统一网格的子项。

The problem is, if my uniformgrid is 5x5, and I have over 25 files, 26th element does not be shown.

问题是,如果我的 uniformgrid 是 5x5,并且我有超过 25 个文件,则不会显示第 26 个元素。

I'd like to know, is there a way to scroll the content of uniform grid?

我想知道,有没有办法滚动统一网格的内容?

I know I can use a listbox and binding (I'm still reading about it), but I need to add the controls programmatically, 'cause the control has an event, and I'm subscribing to it when a new instance of the usercontrol is created and then added to the child array.

我知道我可以使用列表框和绑定(我仍在阅读它),但是我需要以编程方式添加控件,因为控件有一个事件,并且当用户控件的新实例出现时我正在订阅它被创建,然后添加到子数组中。

I've seen thispost, and I already placed the uniforgrid inside a ItemsControl, but It does not work at all, this is my xaml:

我看过这篇文章,我已经将 uniforgrid 放在 ItemsControl 中,但它根本不起作用,这是我的 xaml:

<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" >
    <ItemsControl x:Name="gridArchivos">
        <ItemsControl.ItemsPanel >
            <ItemsPanelTemplate >
                <UniformGrid Columns="5" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>                    
</ScrollViewer>

According to the post, only cols or rows need to be specified, never both. So, only 5 cols. I don't want hrizontal scrolling, only vertical.

根据帖子,只需要指定 cols 或 rows ,而不是同时指定。所以,只有 5 列。我不想要水平滚动,只想要垂直滚动。

Thanks for your time.

谢谢你的时间。

回答by sa_ddam213

I copied your Xamland it seems to work as expected

我复制了你的Xaml,它似乎按预期工作

Here is my test code incase it helps you diagnose your problem

这是我的测试代码,它可以帮助您诊断问题

Xaml:

Xml:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" >
        <ItemsControl ItemsSource="{Binding Items, ElementName=UI}">
            <ItemsControl.ItemsPanel >
                <ItemsPanelTemplate >
                    <UniformGrid Columns="5"  />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </ScrollViewer>
</Window>

Code:

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        for (int i = 0; i < 1000; i++)
        {
            Items.Add("Stackoverflow"+i);
        }
    }

    private ObservableCollection<string> items = new ObservableCollection<string>();
    public ObservableCollection<string> Items
    {
        get { return items; }
        set { items = value; }
    }
}

Result:

结果:

enter image description here

在此处输入图片说明