wpfmarginwpfdatagriddatagridtextcolumn

How to set margin for DataGridTextColumn and text inside it?


i create a DataGrid:

<DataGrid x:Name="dataGrid"
                                  AutoGenerateColumns="False"
                                  Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
                                  IsEnabled="True"
                                  IsReadOnly="False"
                                  CanUserAddRows="False"
                                  CanUserDeleteRows="False"
                                  Loaded="dgAttfs_Loaded"
                                  RowEditEnding="dataGrid_RowEditEnding"

                                  ItemsSource="{Binding ViewModel.RowColl,Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
                                  >

                            <DataGrid.Columns>
                                <DataGridTextColumn Binding="{Binding Name}" MinWidth="120" Width="Auto                                                             
                                                    Header="Name" 
                                                    />
                                <DataGridComboBoxColumn x:Name="typeColumn" MinWidth="220"                                       
                                    SelectedItemBinding="{Binding Path=Type}"                                        
                                    Header="Type"                                                          
                                                        >
                                    <DataGridComboBoxColumn.ItemsSource>
                                        <col:ArrayList>
                                            <sys:String>1</sys:String>
                                            <sys:String>2</sys:String>
                                            <sys:String>3</sys:String>
                                            <sys:String>4</sys:String>
                                            <sys:String>5</sys:String>
                                            <sys:String>6</sys:String>
                                            <sys:String>7</sys:String>
                                            <sys:String>8</sys:String>
                                        </col:ArrayList>
                                    </DataGridComboBoxColumn.ItemsSource>                                                                              
                                </DataGridComboBoxColumn>                                    
                            </DataGrid.Columns>                                                                                           
                        </DataGrid>

But, i can not understand, how to set margin into column text (from left side). I mean, that text tight to the table (left side). Can i set margin to text?


Solution

  • You can create a custom template, with a DataGridTemplateColumn, and in its DataTemplate you can put a textblock with the margin that you want.

      <DataGrid Name="dgUsers" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
            <DataGridTemplateColumn Header="Birthday">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Textblock Text="{Binding Name}" BorderThickness="0" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    Source