2011-03-08 53 views
1

我在WPF電網提出了一些數據如下:如何更好地在WPF網格中呈現這些數據?

 <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" MinWidth="150"/> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition MinWidth="200"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 
      <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="5" Style="{StaticResource SectionNumber}" Content="1.3"/> 
      <Label Grid.Row="1" Grid.Column="0" Margin="5 2">Number of grid in X:</Label> 
      <Label Grid.Row="1" Grid.Column="1" Margin="5 2">NX</Label> 
      <lib:NumericTextBox Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Margin="5 2" Text="{Binding Parameters.NX}"/> 
      <Label Grid.Row="2" Grid.Column="0" Margin="5 2">Number of grid in Y:</Label> 
      <Label Grid.Row="2" Grid.Column="1" Margin="5 2">NY</Label> 
      <lib:NumericTextBox Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" Margin="5 2" Text="{Binding Parameters.NY}"/> 
     <Label Grid.Row="3" Grid.Column="0" Margin="5 2">Type of data:</Label> 
     <Label Grid.Row="3" Grid.Column="1" Margin="5 2">MD</Label> 
     <ComboBox x:Name="cmbMD" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Margin="5 2" SelectedItem="{Binding Data.MD}"/> 
     <Label Grid.Row="4" Grid.Column="0" Margin="5 2">Data value:</Label> 
     <Label Grid.Row="4" Grid.Column="1" Margin="5 2">D</Label> 
     <TextBox Grid.Row="4" Grid.Column="2" Margin="5 2" Text="{Binding Data.D}"/> 
     <TextBox Grid.Row="4" Grid.Column="3" Margin="5 2" Text="{Binding Unit.LengthUnit, Mode=OneWay}" IsReadOnly="True" /> 
... 

的問題是,我有數百行來寫。在WinForms中,我可能會編寫一個用戶控件來呈現一行數據,而我只需要一行代碼來創建一行。

我怎樣才能在這裏簡化它? DataTemplate可以幫助嗎?我知道我可以在列表框中使用DataTemplate使其更容易。但是,我需要將列垂直對齊,所以我認爲Grid是唯一的出路,對吧?

回答

1

WPF ListView或DataGrid是要走的路。

我正在爲你寫一個簡單的DataGrid XAML。

<DataGrid ItemsSource="{Binding YourCollection}" > 
<DataGrid.Columns> 
    <DataGridTemplateColumn Header="YourColumnHeader1" > 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <Label Content="{Binding Property1}" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 

    <DataGridTemplateColumn Header="YourColumnHeader2" > 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <lib:NumericTextBox Text="{Binding Property2}" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 

</DataGrid.Columns> 
</DataGrid> 
+0

感謝您的提示。問題是我在第二列中有不同類型的列,例如文本框,comobobox,複選框和一些行在單元結尾處有一個額外的列。我該如何切換?有沒有辦法使用trigers來做到這一點?我只是用更多的數據更新了我的問題。 – newman 2011-03-08 04:40:54

+0

請參閱示例中的DataTemplate,您可以根據類型具有不同的內容,或者可以在其上包含DataTemplate.Triggers。 DataGrid完全是爲了這個目的,我建議你在DataGrid上閱讀更多內容。 – 2011-03-08 05:45:48

+1

再次感謝提示。我在http://www.wpftutorial.net/DataTemplates.html找到了一個很好的例子。關鍵是要使用DataTemplateSelector。 – newman 2011-03-09 15:39:07

1

這聽起來像你想要的是DataGrid,而不是Grid

+0

我用更多的樣本數據更新了我的問題。我怎麼能把不同類型的控件(文本框,組合框,複選框)放入第二列? – newman 2011-03-08 04:53:54