Reputation: 6911
I have some data presented in WPF Grid as follows:
<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" />
...
The problem is that I have hundreds of rows to write. In WinForms, I would probably write a user control to present a row of data, and I just need one line of code to create a row.
How can I simplify it here? Can DataTemplate help somehow? I know i can use DataTemplate in a listbox to make it easier. However, I need the columns to be aligned vertically, so I think Grid is the only way to go, right?
Upvotes: 1
Views: 446
Reputation: 50038
WPF ListView or DataGrid is the way to go.
I am writing a simple DataGrid XAML for you.
<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>
Upvotes: 1