2016-01-19 5 views
3

Я хочу добавить 225 (15x15) кнопок на моей сетке. Я создал строки и столбцы. Затем я создал динамические кнопки. Я пытаюсь как этотДобавить динамические кнопки в сетке

MainWindow.xaml

<Window x:Class="WpfApplication15.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="500" Width="500"> 
    <Grid Name="gridMain" ShowGridLines="True" > 

     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 


    </Grid> 
</Window> 

MainWindow.xaml.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication15 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      for (int i = 0; i < 15; i++) 
      { 
       for (int j = 0; j < 15; j++) 
       { 
        Button MyControl = new Button(); 
        MyControl.Content = i.ToString(); 
        MyControl.Name = "Button" + i.ToString(); 

        gridMain.SetValue(Grid.RowProperty, j); 
        gridMain.Children.Add(MyControl); 

       } 
       gridMain.SetValue(Grid.ColumnProperty, i); 
      } 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      Button b = (Button)sender; 
     } 


    } 
} 

Но я вижу это:

enter image description here

Когда я хочу см. это:

enter image description here

+0

Все, кажется, хорошо, вам просто нужно установить строки и столбца с использованием сетки класса т.е. "Grid.SetRow (someLabel, 0);". @Wimmel дал отличный ответ –

+0

Спасибо, я решу это –

ответ

1

Спасибо!

Я решил это

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication15 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      int count = 1; 


      for (int i = 0; i < 15; i++) 
      { 
       for (int j = 0; j < 15; j++) 
       { 
        Button MyControl1 = new Button(); 
        MyControl1.Content = count.ToString(); 
        MyControl1.Name = "Button" + count.ToString(); 

        Grid.SetColumn(MyControl1, j); 
        Grid.SetRow(MyControl1, i); 
        gridMain.Children.Add(MyControl1); 

        count++; 
       } 

      } 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      Button b = (Button)sender; 

     } 


    } 
} 

enter image description here

Смежные вопросы