0

我是新的Windows應用程序更新發育:動態設置按鈕的寬度和高度

我有一種情況,我需要用下面的文本顯示按鈕的矩陣,我能夠做到這一點,但這裏的問題是矩陣可以是任何事物2x2,2x3,2x4或2x6。

但按鈕應該是正方形而不是矩形,如果我將圖像添加到它然後圖像看起來拉伸。

這裏是我的代碼:

public partial class MainPage : PhoneApplicationPage 
    { 

     int numberOfColumns = 2; 
     int numberOfRows = 3; 
     public double cellWidth; 
     public double cellHeight; 

     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 

      this.Loaded += new RoutedEventHandler(SetGridCellWidthAndHeight); 

     } 

     void SetGridCellWidthAndHeight(object sender, RoutedEventArgs e) 
     { 
      cellWidth = GridWindows.ActualHeight/numberOfColumns; 
      cellHeight = GridWindows.ActualHeight/numberOfRows; 
      this.GridWindows.Children.Add(SetUpGridLayout()); 
     } 

     private Grid SetUpGridLayout() 
     { 
      Grid grid = new Grid(); 
      grid.Background = new SolidColorBrush(Colors.White); 

      // Create column and row definitions. 
      ColumnDefinition[] columnDefinition = new ColumnDefinition[numberOfColumns]; 
      RowDefinition[] rowDefinition = new RowDefinition [numberOfRows]; 

      for (int i = 0; i < columnDefinition.Count(); i++) 
      { 
       columnDefinition[i] = new ColumnDefinition(); 
       grid.ColumnDefinitions.Add(columnDefinition[i]); 
      } 

      for (int i = 0; i < rowDefinition.Count(); i++) 
      { 
       rowDefinition[i] = new RowDefinition(); 
       grid.RowDefinitions.Add(rowDefinition[i]); 
      } 

      int count = 1; 

       for (int row = 0; row < numberOfRows; row++) 
       { 
        for (int column = 0; column < numberOfColumns; column++) 
        { 
         StackPanel gridViewStackPlanel = new StackPanel(); 
         gridViewStackPlanel.Background = new SolidColorBrush(Colors.White); 

         Button button = new Button(); 
         button.Width = cellWidth*0.8; 
         button.Height = cellHeight *0.8; 
         //topicButton.Background = new SolidColorBrush(Colors.Red); 
         button.VerticalAlignment = VerticalAlignment.Center; 
         button.HorizontalAlignment = HorizontalAlignment.Center; 
         button.Background = new SolidColorBrush(Colors.Red); 

         //To display the Topic name 
         TextBlock name= new TextBlock(); 
         name.Text = " Value" + count; 
         name.Foreground = new SolidColorBrush(Colors.Black); 
         name.HorizontalAlignment = HorizontalAlignment.Center; 

         gridViewStackPlanel.Children.Add(button); 
         gridViewStackPlanel.Children.Add(name); 

         count++; 

         grid.Children.Add(gridViewStackPlanel); 
         Grid.SetColumn(gridViewStackPlanel, column); 
         Grid.SetRow(gridViewStackPlanel, row); 
        } 
       } 
      return grid; 

     } 

回答

4

當你寫這樣的代碼:

button.Width = cellWidth * 0.8; 
button.Height = cellHeight * 0.8; 

你會得到一個正方形只有cellWidth == cellHeight。這很可能不是真的。所以你的寬度和高度是不同的。考慮用這樣的東西代替上述:

cellWidth = Math.Min(GridWindows.ActualHeight/numberOfColumns, GridWindows.ActualHeight/numberOfRows); 

button.Width = cellWidth * 0.8; 
button.Height = cellWidth * 0.8; 

現在它將是方形的。