Text alignment in DataGrid

You need set DataGridCell style <DataGrid> <DataGrid.Columns> <DataGridTextColumn> <DataGridTextColumn.ElementStyle> <Style TargetType=”TextBlock”> <Setter Property=”HorizontalAlignment” Value=”Center” /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid>

DataGrid row content vertical alignment

Complete solution of this issue at MSDN: Vertical alignment of DataGrid row content. In brief, in style-file set: <!–body content datagrid cell vertical centering–> <Style x:Key=”Body_Content_DataGrid_Centering” TargetType=”{x:Type DataGridCell}”> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”{x:Type DataGridCell}”> <Grid Background=”{TemplateBinding Background}”> <ContentPresenter VerticalAlignment=”Center” /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> In window file: <DataGrid x:Name=”ContentDataGrid” Style=”{StaticResource ContentDataGrid}” CellStyle=”{StaticResource Body_Content_DataGrid_Centering}” ItemsSource=”{Binding}” … Read more

Adding a Button to a WPF DataGrid

First create a DataGridTemplateColumn to contain the button: <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Click=”ShowHideDetails”>Details</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> When the button is clicked, update the containing DataGridRow‘s DetailsVisibility: void ShowHideDetails(object sender, RoutedEventArgs e) { for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual) if (vis is DataGridRow) { var row = … Read more

Select multiple items from a DataGrid in an MVVM WPF project

You can simply add a custom dependency property to do this: public class CustomDataGrid : DataGrid { public CustomDataGrid () { this.SelectionChanged += CustomDataGrid_SelectionChanged; } void CustomDataGrid_SelectionChanged (object sender, SelectionChangedEventArgs e) { this.SelectedItemsList = this.SelectedItems; } #region SelectedItemsList public IList SelectedItemsList { get { return (IList)GetValue (SelectedItemsListProperty); } set { SetValue (SelectedItemsListProperty, value); } } … Read more

How get a WPF Datagrid with cells that wrap text instead of truncating it?

Thanks for your help @H.B., this did the trick for me (alignment is optional): <DataGrid.Columns> <DataGridTextColumn Header=”Wrapped & centered” Binding=”{Binding field}”> <DataGridTextColumn.ElementStyle> <Style> <Setter Property=”TextBlock.TextWrapping” Value=”Wrap” /> <Setter Property=”TextBlock.TextAlignment” Value=”Center”/> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> </DataGrid.Columns>

How to get a WPF DataGrid cell to right align without making the selectable area on a new row tiny?

You could apply ElementStyle on the DataGridTextColumn targetted to TextBlock and right align that, it would work. <DataGridTextColumn Binding=”{Binding Path=ImpaId}”> <DataGridTextColumn.Header> <TextBlock Style=”{StaticResource DataGridHeader}”> Impa </TextBlock> </DataGridTextColumn.Header> <DataGridTextColumn.ElementStyle> <Style TargetType=”{x:Type TextBlock}”> <Setter Property=”HorizontalAlignment” Value=”Right” /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn>

Set a padding on dataGridCells in WPF

The problem is that the Padding isn’t transfered to the Border that’s in the Template for DataGridCell. You can edit the Template and add the TemplateBinding for Padding <DataGrid …> <DataGrid.CellStyle> <Style TargetType=”DataGridCell”> <Setter Property=”Padding” Value=”20″/> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”{x:Type DataGridCell}”> <Border Padding=”{TemplateBinding Padding}” BorderBrush=”{TemplateBinding BorderBrush}” BorderThickness=”{TemplateBinding BorderThickness}” Background=”{TemplateBinding Background}” SnapsToDevicePixels=”True”> <ContentPresenter SnapsToDevicePixels=”{TemplateBinding SnapsToDevicePixels}”/> … Read more