WPF DataGridCell保证金

时间:2014-09-24 16:35:35

标签: c# wpf wpfdatagrid

我有以下数据网格:

<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中设置它。我知道Margin必须在DataGridCell上设置(通过使用Snoops找到),但不知道如何在<DataGridTemplateColumn.CellTemplate>中实现

1 个答案:

答案 0 :(得分:1)

级联风格:

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