我正在使用可选行渲染表格。最终进入的数据将来自数据库的数据绑定,但现在我只想尝试显示一行。这就是我所拥有的:
<Grid Background="WhiteSmoke">
<StackPanel>
<Grid Width="900">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Background" Value="White" />
<Setter Property="Padding" Value="5" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</Grid.Resources>
<Border Grid.Column="0" Grid.Row="1">
<TextBlock Text="Status" />
</Border>
<Border Grid.Column="1" Grid.Row="1">
<TextBlock Text="Work Package" />
</Border>
<Border Grid.Column="2" Grid.Row="1">
<TextBlock Text="Description" />
</Border>
<Border Grid.Column="3" Grid.Row="1">
<TextBlock Text="Foreman" />
</Border>
<Border Grid.Column="4" Grid.Row="1">
<TextBlock Text="Field Issue" />
</Border>
<Border Grid.Column="5" Grid.Row="1">
<TextBlock Text="Start Date" />
</Border>
<Border Grid.Column="6" Grid.Row="1">
<TextBlock Text="Finish Date" />
</Border>
</Grid>
<ListBox >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="900">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Background" Value="White" />
<Setter Property="Padding" Value="5" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</Grid.Resources>
<Border Grid.Column="0" Grid.Row="1">
<TextBlock Text="1" />
</Border>
<Border Grid.Column="1" Grid.Row="1">
<TextBlock Text="2" />
</Border>
<Border Grid.Column="2" Grid.Row="1">
<TextBlock Text="3" />
</Border>
<Border Grid.Column="3" Grid.Row="1">
<TextBlock Text="4" />
</Border>
<Border Grid.Column="4" Grid.Row="1">
<TextBlock Text="5" />
</Border>
<Border Grid.Column="5" Grid.Row="1">
<TextBlock Text="6" />
</Border>
<Border Grid.Column="6" Grid.Row="1">
<TextBlock Text="7" />
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
正在渲染标题,但第一行应该只有数字值:
我做错了什么?为什么不显示数据行?
答案 0 :(得分:1)
正如我在评论中所述,您尚未向列表框添加任何项目,因此不会显示dataTemplate。
对于测试,您可以在ItemTemplate之后添加项目:
</ListBox.ItemTemplate>
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
</ListBox>