WPF Scroll a Uniformgrid
Asked Answered
P

1

6

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.

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 this post, and I already placed the uniforgrid inside a ItemsControl, but It does not work at all, this is my 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.

Thanks for your time.

Pencil answered 11/7, 2013 at 0:43 Comment(0)
D
8

I copied your Xaml and it seems to work as expected

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

Xaml:

<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

Diplegia answered 11/7, 2013 at 1:3 Comment(3)
With your example, I've found that I need to add a lot of items to see the grid the way I want. Is there a way to keep the items arranged like a 5x5 matrix? With 5 items, the grid displays them centered on vertical, and I'd like them at the top. Thanks for your help!!Pencil
Nevermind, I've found it, I added VerticalAlignment="Top" to the uniformgrid attributes. Thanks for your help!Pencil
What if I want to define the rows too? Defining the number of the columns and VerticalAlignment="Top" it works as in the example above, but not if I define the rows as wellFlyblow

© 2022 - 2024 — McMap. All rights reserved.