WPF DataGridCell Margin

巧了我就是萌 提交于 2021-02-10 07:49:05

问题


I have the following data grid:

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

Applied to this data grid is the following style:

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

This style adds 5 pixels of spacing to the left of the cell contents and centers the text vertically.

I would like to set the margin of just the first column's cells (<DataGridTemplateColumn Header="">) to 0. How can I set this in the DataTemplate. I know the Margin has to be set on DataGridCell (found by using Snoops) but do not know how to implement in the <DataGridTemplateColumn.CellTemplate>


回答1:


Cascade the style:

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


来源:https://stackoverflow.com/questions/26022106/wpf-datagridcell-margin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!