2013-01-11 73 views
1

我有一个数据网格下面给出:WPF数据网格的高度和宽度的调整问题

<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> 

这是模板列的样式:在datagridcellstyle我已确定的细胞的值作为宽度

<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> 

虽然和高度为50.但是,当我加载9行datagrid它的高度显示452而不是450(9 * 50),并以同样的方式显示更多的宽度。
为什么它显示像那样?
如何避免?

回答

1

可能是因为有一些由模板内的默认填充或保证金。您可以尝试将单元格的填充值设置为0,或者可以在Expression Blend中的数据网格的内部控件模板中看到空间来自哪里。

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>