wpf datagrid高度和宽度调整问题

时间:2013-01-11 04:13:17

标签: wpf datagrid height width padding

我有一个数据网格,如下所示:

    <DataGrid  SizeChanged="dgvMap_SizeChanged" Padding="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" GridLinesVisibility="None" Background="Transparent" 
                                       BorderBrush="Transparent" IsReadOnly="True" ItemsSource="{Binding IsAsync=True}"  EnableColumnVirtualization="True" 
                                       EnableRowVirtualization="True" AutoGenerateColumns="True" AutoGeneratingColumn="dgvMap_AutoGeneratingColumn" 
                                       CanUserAddRows="False" CanUserSortColumns="true" CanUserDeleteRows="False" HeadersVisibility="None" 
                                       Name="dgvMap" SelectionMode="Single" Panel.ZIndex="0" Margin="0,0,0,0" VirtualizingStackPanel.VirtualizationMode="Standard" 
                                       PreviewMouseDown="dgvMap_PreviewMouseDown" >
                                <!--for removing the blue color bkground default for row selection-->
                                <DataGrid.Resources>
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                                </DataGrid.Resources>
                                <DataGrid.CellStyle>
                                    <Style TargetType="DataGridCell">
                                        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                        <Setter Property="Padding" Value="0"/>
                                        <Setter Property="Height" Value="50" />
                                        <Setter Property="Width" Value="50" />
                                    </Style>
                                </DataGrid.CellStyle>
                            </DataGrid>  

这是模板列的样式:

<DataTemplate x:Key="MyDataTemplate" DataType="DataRowView">
            <StackPanel Background="Transparent">
                <Image Tag="{Binding}" Name="Layer0" Margin="0,0,0,0"  Panel.ZIndex="1"  
                        ToolTipService.HasDropShadow="True" ToolTipService.ShowDuration="20000" ToolTipService.InitialShowDelay="200" >

                <Image.Resources>
                    <Style TargetType="{x:Type Image}">
                        <Setter Property="Source" Value="{Binding Converter={StaticResource IntToImageConverter}, ConverterParameter = Layer0}" />
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <!-- Hover image -->
                                <Setter Property="Cursor" Value="Hand"/>
                                <Setter Property="Source" Value="D:\small.png"/>

                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Image.Resources>
            </Image>
        </StackPanel>
    </DataTemplate>

虽然在datagridcellstyle我已经将单元格值定义为宽度和高度为50.但是当我将数据网格加载到9行时,其高度显示为452而不是450(9 * 50)并且以相同的方式宽度也是显示更多。
为什么它显示那样?
如何避免?

2 个答案:

答案 0 :(得分:1)

可能是因为模板内部有一些默认的填充或边距。您可以尝试将单元格的填充设置为0,也可以在Expression Blend中查看数据网格内部控件模板中的空间来源。

答案 1 :(得分:0)

我通过给datagridcolumnstyle这样的样式

来解决它
                                <DataGrid.ColumnHeaderStyle>
                                    <Style TargetType="DataGridColumnHeader">
                                        <Setter Property="Padding" Value="0"/>
                                        <Setter Property="Height" Value="50" />
                                        <Setter Property="Width" Value="50" />
                                    </Style>
                                </DataGrid.ColumnHeaderStyle>