Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Scroll a Uniformgrid

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.

like image 575
BlackCath Avatar asked Jul 11 '13 00:07

BlackCath


1 Answers

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

like image 167
sa_ddam213 Avatar answered Oct 17 '22 20:10

sa_ddam213