1
我想直观地表示列中的数据,而不是DataGrid的行表示。您可以将我的数据视为List<Column>
,其中列包含List<Cell>
。我不转置数据并仅使用DataGrid的原因是我希望能够动态添加/删除列,而无需处理所有数据。在自定义wpf数据网格中同步行高
+--------------------------------------------+
| | Col Header | Col Header | ... |
| Row Header | Cell | Cell | ... |
| ... | ... | ... | ... |
+--------------------------------------------+
我有XAML的样本是几乎没有什么我想:
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<TextBox Text="" IsReadOnly="True" BorderThickness="0" HorizontalAlignment="Stretch"/>
<TextBox Text="" IsReadOnly="True" BorderThickness="0,0,0,2" BorderBrush="Black" HorizontalAlignment="Stretch"/>
<ItemsControl ItemsSource="{Binding RowHeaders}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Mode=OneWay}" IsReadOnly="True" BorderThickness="0,0,2,2" BorderBrush="Black" FontWeight="Bold" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
<ItemsControl ItemsSource="{Binding Columns}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<TextBox Text="{Binding ColHead1, Mode=OneWay}" IsReadOnly="True" BorderThickness="0" FontWeight="Bold" HorizontalAlignment="Stretch"/>
<TextBox Text="{Binding ColHead2, Mode=OneWay}" IsReadOnly="True" FontWeight="Bold" BorderThickness="0,0,0,2" BorderBrush="Black" HorizontalAlignment="Stretch"/>
<ItemsControl ItemsSource="{Binding Cells}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox x:Name="textBox" Text="{Binding}" IsReadOnly="{Binding IsReadOnly}" BorderThickness="0,0,0,2" BorderBrush="Black" HorizontalAlignment="Stretch"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
但是,我怎么能保证我行始终是对齐的?由于列中的单元格包含在单个ItemsControl中,因此保证它们是对齐的。但是,每个列都位于自己的StackPanel内。有没有办法确保所有的行都对齐?
此外,这似乎是一个很好的解决方案,或者是否有更好的选择?
您应该能够使用[Grid.IsSharedSizeScope](HTTP ://msdn.microsoft.com/en-us/library/system.windows.controls.grid.issharedsizescope.aspx)使您的行共享一个大小。过去我曾经使用过列,但没有理由它不能用于行。 – Rachel 2012-02-01 04:12:26
@Rachel这实际上有帮助。添加它作为答案,以便我可以接受它。谢谢。 – Rado 2012-02-01 19:44:05
完成,谢谢:) – Rachel 2012-02-01 20:38:30