Fernando Santiago
Fernando Santiago

Reputation: 2288

How can i get all the buttons in the uniformgrid and scroll it to see them all?

i am building an application to show all the software installed in the computer, i already have all the buttons to show with the respective icon, but when i show them, the uniformgrid only shows the buttons that fit in to the window, i thought a scrollbar will show them, but i get to the end of the window and the buttons still missing! how can i show them all with a scrollbar? Here is the XAML code:

<Window x:Class="apple.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"> 
        <Grid>
            <DockPanel Name="dock">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <UniformGrid Name="gridx" DockPanel.Dock="Top" Rows="7" Columns="7">

                </UniformGrid>
            </ScrollViewer>
        </DockPanel>
    </Grid>
</Window>

Here is the c# code:

namespace apple
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string[] link = Directory.GetFiles(@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs", "*.lnk", SearchOption.AllDirectories);

        public MainWindow()
        {
            this.ResizeMode = ResizeMode.NoResize;
            //this.WindowStyle = WindowStyle.None;
            this.WindowState = WindowState.Maximized;
            InitializeComponent();
            masterGUI();
        }

        public void masterGUI()
        {
            gridx.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            IconImage[] ico = null;
            Bitmap[] img = null;
            string[] list = null;
            list = new string[link.Length];
            ico = new Icon[link.Length];
            img = new Bitmap[link.Length];
            for (int n = 0; n < link.Length; n++)
            {
                ImageBrush ib = new ImageBrush();
                System.Windows.Controls.Button newBtn = new Button();
                list[n] = System.IO.Path.GetFileNameWithoutExtension(link[n]);
                FileToImageIconConverter some = new FileToImageIconConverter(link[n]);
                ImageSource imgSource = some.Icon;
                ib.ImageSource = imgSource;
                newBtn.Name = "a" + n;
                newBtn.Background = ib;
                newBtn.Content = list[n];
                newBtn.Click += new RoutedEventHandler(newBtn_Click);
                gridx.Children.Add(newBtn);
            }  
        }

        private void newBtn_Click(object sender, RoutedEventArgs e)
        {
            Button clicked = (Button)sender;
            string test = null;
            test = clicked.Name.Replace("a","0");
            this.Close();
            System.Diagnostics.Process.Start(link[Int32.Parse(test)]);
        }
    }
}

Upvotes: 1

Views: 1771

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178760

Remove the Grid and DockPanel and set either UniformGrid.Rows or UniformGrid.Columns, not both. All you need is Window, ScrollViewer, and UniformGrid:

<Window>
    <ScrollViewer>
        <UniformGrid Name="gridx" Columns="7"/>
    </ScrollViewer>
</Window>

And to do it in a more idiomatic WPF fashion, you should have something like this:

<Window>
    <ScrollViewer>
        <ItemsControl ItemsSource="{Binding Programs}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="7"/>

You would then expose a Programs collection from your data source and would thus automatically generate an item for each installed program.

Upvotes: 3

Related Questions