2010-12-13 8 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); 
     } 
    }