2010-12-13 10 views
0

テーブルを作成して、KEY VALUEの種類を表示する必要があります。WPF:Gridの動的ラベル追加

私は以下のコードを試しましたが、オーバーラッピング出力と混乱しました。私は、グリッドのRowDefinitionsとColumnDefinitionsを作成する必要があるが、それを達成することはできないと考えている。私を助けてください。

XAML:背後

<Window x:Class="GrideLabel.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"> 
    <Grid Name="LabelGrid"></Grid> 
</Window> 

コード:

あなたが行と列の定義を定義し、子コントロールに行と列を割り当てる必要があり
public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      AddLabelDynamically(); 
     } 

     private void AddLabelDynamically() 
     { 
      for (int i = 0; i < 3; i++) 
      { 
       Label nameLabel = new Label(); nameLabel.Content = "KEY :"+i.ToString(); 
       Label dataLabel = new Label(); dataLabel.Content = "VALUE :"+i.ToString(); 
       //I want to creatre the Seperate coloum and row to display KEY 
       // VALUE Pair distinctly 
       this.LabelGrid.Children.Add(nameLabel); 
       this.LabelGrid.Children.Add(dataLabel); 
      } 
     } 
    } 

答えて

0

。次のコードでは、次のことが行われます。

private void AddLabelDynamically() 
    { 
     this.LabelGrid.ColumnDefinitions.Clear(); 
     this.LabelGrid.RowDefinitions.Clear(); 

     this.LabelGrid.ColumnDefinitions.Add(new ColumnDefinition()); 
     this.LabelGrid.ColumnDefinitions.Add(new ColumnDefinition()); 

     for (int i = 0; i < 3; i++) 
     { 
      this.LabelGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); 

      Label nameLabel = new Label(); nameLabel.Content = "KEY :" + i.ToString(); 
      Label dataLabel = new Label(); dataLabel.Content = "VALUE :" + i.ToString(); 

      Grid.SetRow(nameLabel, i); 
      Grid.SetRow(dataLabel, i); 

      Grid.SetColumn(nameLabel, 0); 
      Grid.SetColumn(dataLabel, 1); 

      //I want to creatre the Seperate coloum and row to display KEY 
      // VALUE Pair distinctly 
      this.LabelGrid.Children.Add(nameLabel); 
      this.LabelGrid.Children.Add(dataLabel); 
     } 
    } 
関連する問題