jeudi 14 avril 2016

Scrollable function with auto generate items grid?

I have a grid which can auto generate checkbox items and i want to implement a horizontal scroll inside. The grid has a fix margin and the horizontal scroll will appear when the number of checkbox items are larger than the width of the grid. Here the grid code

<Grid x:Name="show"                  
                   Height="Auto"
                   Width="Auto"
                   Margin="140,40,100,0">                
                </Grid>

And here is the checkbox items generate code:

public void create(int rows, int columns)
        {
            show.Children.Clear();           
            Grid newGrid = new Grid();

            for (int i = 0; i < rows; i++)
            {
                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = new GridLength(75);
                newGrid.RowDefinitions.Add(rowdef);

            }
            for (int i = 0; i < columns; i++)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = new GridLength(10);
                newGrid.ColumnDefinitions.Add(coldef);
            }
            int j = 0;
            for (int x = 0; x < rows; x++)
                for (int y = 0; y < columns; y++)
                {
                 {                      
                        CheckBox box = new CheckBox();
                        box.Width = 100;
                        box.Height = 100;
                        Grid.SetColumn(box, y);
                        Grid.SetRow(box, x);
                        newGrid.Children.Add(box);
                 }
                    j = j + 1;
                }

            show.Children.Add(newGrid);
            show.Visibility = Visibility.Visible;
        }

How can I do it ? And is it possible to do so using the code view instead of the design view ? I already tried ScrollViewer function but it is still no avail...




Aucun commentaire:

Enregistrer un commentaire