2013-01-11 107 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>