在Datagrid行上填充

时间:2011-12-12 21:00:46

标签: wpf datagrid

我有一个带有文本列的WPF数据网格,两个模板列和两个复选框列 模板列垂直居中对齐(顶部和底部填充相等),文本列顶部有一个小填充,但底部有很多填充,而复选框顶部只有大约1个像素填充。

我想将所有这些中心垂直对齐,但我不想制作模板列来保存复选框和文本框。

这样做的正确方法是什么?

编辑:XAML

        <DataGrid AutoGenerateColumns="False" Name="dgROList" ItemsSource="{Binding ElementName=MainWindow, Path=cROInfo}" CanUserDeleteRows="True" CanUserReorderColumns="False" GridLinesVisibility="Horizontal" Margin="0,0,0,0" Grid.ColumnSpan="2" AlternatingRowBackground="#FFFFE776">
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Name="mnuDelete" Header="Delete" />
                </ContextMenu>
            </DataGrid.ContextMenu>
            <DataGrid.Columns>
                <DataGridTextColumn Header="RO Number" Width="Auto" Binding="{Binding RONum}" />
                <DataGridTemplateColumn x:Name="roDetails" Header="RO Details" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl Name="LineDetails" ItemsSource="{Binding LineInfo}" Width="Auto" Grid.IsSharedSizeScope="True">
                                <ItemsControl.Template>
                                    <ControlTemplate TargetType="ItemsControl">
                                        <ItemsPresenter />
                                    </ControlTemplate>
                                </ItemsControl.Template>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Vertical" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="20" />
                                                <ColumnDefinition Width="Auto" SharedSizeGroup="DetailCol"/>
                                                <ColumnDefinition Width="50" />
                                                <ColumnDefinition Width="150*"/>
                                            </Grid.ColumnDefinitions>
                                            <Label Content="{Binding Line}" Grid.Column="0" />
                                            <Label Content="{Binding Status}" Grid.Column="1" />
                                            <Label Content="{Binding AmountPaid}" Grid.Column="2" />
                                            <Label Content="{Binding SDate}" Grid.Column="3" />
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn x:Name="roRI" Header="RI" Width="Auto" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding RI}" ToolTipService.ShowDuration="60000" ></Label>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridCheckBoxColumn Header="Paid" Width="40" Binding="{Binding Paid}" />
                <DataGridCheckBoxColumn Header="Adj" Width="30" Binding="{Binding Adjustment}" />
            </DataGrid.Columns>
        </DataGrid>

编辑2:实际上,我甚至不需要垂直对齐它们。只需在顶部添加一个小填充,这样它就会与模板列的顶部对齐。

1 个答案:

答案 0 :(得分:2)

你尝试过这个:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="VerticalAlignment" Value="Center" />
</Style>