2014-09-24 124 views
1

我有以下數據格:WPF DataGridCell保證金

<DataGrid Name="PropertiesDataGrid" 
      ItemsSource="{Binding PropertiesDataView, UpdateSourceTrigger=PropertyChanged}" 
      SelectedItem="{Binding SelectedProperty, Mode=TwoWay, 
         UpdateSourceTrigger=PropertyChanged}" 
      AutoGenerateColumns="False" 
      CanUserAddRows="False" 
      MaxHeight="200"> 
    <i:Interaction.Behaviors> 
     <helper:ScrollIntoViewBehavior/> 
    </i:Interaction.Behaviors> 

    <DataGrid.Columns> 
     <DataGridTemplateColumn Header=""> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <Button Command="Delete" 
          Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"> 
         <Image Source="../Resources/red_x.ico" 
           Height="15" /> 
        </Button> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 

     <DataGridTextColumn Header="ID" 
          Width="50" 
          Binding="{Binding ID}" 
          ElementStyle="{StaticResource CenterTextCellStyle}" 
          IsReadOnly="True" /> 

     <DataGridTextColumn Header="PropertyName" 
          Width="*" 
          Binding="{Binding PropertyName}" 
          ElementStyle="{StaticResource LeftTextCellStyle}" /> 

     <DataGridTextColumn Header="PropertyValue" 
          Width="300" 
          Binding="{Binding PropertyValue}" 
          ElementStyle="{StaticResource LeftTextCellStyle}" /> 
    </DataGrid.Columns> 

</DataGrid> 

應用於此數據網格是以下樣式:

<Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="Height" Value="22" /> 
    <Setter Property="Margin" Value="5,0,0,0" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Grid Background="{TemplateBinding Background}"> 
        <ContentPresenter VerticalAlignment="Center" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

此樣式增加了間距的5個像素到該單元的左內容並垂直居中文本。

我想將第一列的單元格(<DataGridTemplateColumn Header="">)的邊距設置爲0.如何在DataTemplate中設置此值。我知道保證金必須在DataGridCell(通過使用窺探找到)設置,但不知道如何在<DataGridTemplateColumn.CellTemplate>

回答

1

級聯實現風格:

<DataGridTemplateColumn Header=""> 
    <DataGridTemplateColumn.CellStyle> 
     <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}"> 
      <Setter Property="Margin" Value="0" /> 
     </Style> 
    </DataGridTemplateColumn.CellStyle> 
+0

完美!謝謝! – BrianKE 2014-09-25 13:12:15