How to center the content of cells in a data grid? - wpf

I set the min height of a datagrid that way:
<DataGrid MinRowHeight="40">
After feeding the datagrid with datas, the text in each cell is top and left aligned.
I could not find an easy way to center that text.
Any suggestions for doing that?

Final solution:
<Style x:Key="DataGridContentCellCentering" 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>

The following code will center the contents of cells in DataGrid:
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>

use ElementStyle
<DataGridTextColumn ElementStyle="{StaticResource Centering}"/>
<Style x:Key="Centering" TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>

you can use styles. i add a sample for DataGridCheckBoxColumn, i hope it put you in the right direction.
<DataGridCheckBoxColumn Header="is active" IsReadOnly="False">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
<DataGridCheckBoxColumn.Binding>
<Binding Path="ISACTIVE" ValidatesOnDataErrors="True" Converter="{StaticResource MyBoolToIsActiveConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"
NotifyOnValidationError="True" ValidatesOnExceptions="True">
</Binding>
</DataGridCheckBoxColumn.Binding>
</DataGridCheckBoxColumn>

Try setting the DataGrid's Vertical and HorizontalContentAlignment to Center
<DataGrid VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
If that doesn't work, you can use the solution in this answer. It uses a style that aligns the DataGrid cell contents

This style will set the VerticalAlignment to Center for all DataGridCells without needing to be applied explicitly.
<Style TargetType="DataGridCell">
<Style.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>
I find this to be the most elegant solution.

Modify your style tag like this.....
<Style x:Key="CellTextCentre" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"></Setter>
<Setter Property="TextBlock.VerticalAlignment" Value="Center"></Setter>
</Style>

This below code display datagrid cell conent in center..
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
<!--DISPLAY CONTENT IN MIDDLE-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Here is a slightly longer version of Arpit Shah's answer.
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Rows}">
<DataGrid.Resources>
<Style x:Key="RightAligned" TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn CellStyle="{StaticResource RightAligned}"
Binding="{Binding Path=Quantity, Mode=OneWay}"
Header="Quantity" Width="60" />
<DataGridTextColumn Binding="{Binding Path=Category, Mode=OneWay}"
Header="Category" Width="150" />
</DataGrid.Columns>
</DataGrid>

Here a better approach to center the content vertically:
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {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>

Related

I have ColumnHeaderStyle for Datagrid and Now i don't want to resize Columns

How can I resize datagrid'sColumns?
I have ColumnHeaderStyle for Datagrid and Now i don't can resize Columns!
Please help me!
i treid this code. but it does not work
CanUserResizeColumns="True"
<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridColumnHeader">
<StackPanel >
<DockPanel>
<Image DockPanel.Dock="Left" Source="/Images/Photo.png"/>
<TextBox Name="txtCaption" Text="{TemplateBinding Content}" />
</DockPanel>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataGrid Name="mainGrid" AutoGenerateColumns="False" IsReadOnly="True" AlternatingRowBackground="#FFEBEBFF"
ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}">
</DataGrid>
In your case: you are using ColumnHeaderStyle so it restricts from resizing.
By default CanUserResizeColumns is true.
Instead of using ColumnHeaderStyle use below code:
<DataGridTextColumn Header="Header1" Width="auto" IsReadOnly="True" Binding="{myUserControls:CultureAwareBinding Path=xxxxx}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right"></Setter>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGridTextColumn.HeaderStyle>

WPF DataGrid Cell Horizontal Alignment Glitch

I have an intermittent problem when center aligning text in WPF data grid text columns.
Notice the left alignment on the first cell. The problem corrects itself after waiting a few seconds, but what could cause this? I've seen this on several grids now, but cannot purposefully reproduce the issue in a fresh project. The grids are usually several layers deep in dockpanels, grids, and groupboxes. Here is the XAML used to style the pictured grid:
<Style TargetType="DataGrid">
<Setter Property="GridLinesVisibility" Value="None" />
<Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
<Setter Property="HeadersVisibility" Value="Column" />
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style x:Key="GridRowStyle" TargetType="DataGridRow">
<Setter Property="FontSize" Value="14.667" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="MinHeight" Value="30" />
<Setter Property="Padding" Value="6,2" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
<Style
x:Key="OrderGridRowStyle"
BasedOn="{StaticResource GridRowStyle}"
TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="{x:Static desktopViewModels:DisplayableOrderState.Uncommitted}">
<Setter Property="Background" Value="LightGoldenrodYellow" />
</DataTrigger>
</Style.Triggers>
</Style>
Here is the grid definition:
<DataGrid
x:Name="Orders"
Grid.Row="0"
HorizontalContentAlignment="Center"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserReorderColumns="False"
CanUserSortColumns="False"
CurrentItem="{Binding SelectedOrderViewModel, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="True"
ItemsSource="{Binding OrderViewModels}"
RowStyle="{StaticResource OrderGridRowStyle}"
SelectedItem="{Binding SelectedOrderViewModel, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Order Name" Width="*"/>
<DataGridTextColumn Binding="{Binding CreatedDate}" Header="Created Date" Width ="*"/>
<DataGridTextColumn Width="*" Binding="{Binding Status}" Header="Status" />
</DataGrid.Columns>
</DataGrid>
I have tried explicitly specifying column widths instead of using * widths, but the problem persists. I've tried other methods of centering the grid cells, such as the following, also with no effect.
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

How to make center column text in datagrid WPF?

I have wpf datagrid with many columns in my datagrid..
<DataGrid.Columns>
...
<mui:DataGridTextColumn x:Name="Column27" Width="50" Header="Cabe" Binding="{Binding B4R27,UpdateSourceTrigger=PropertyChanged ,Converter={StaticResource CheckConverter}, Mode=TwoWay}" />
<mui:DataGridTextColumn IsReadOnly="True" x:Name="Column28" Width="50" Header="Jumlah Bahan Pokok" Binding="{Binding B4RJ,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="#A2D1A2" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</mui:DataGridTextColumn>
<mui:DataGridTextColumn x:Name="Column29" Width="150" Header="Tulis Nama Pengusaha" Binding="{Binding B4R28,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" />
<mui:DataGridTextColumn x:Name="Column30" Width="130" Header="Tulis Alamat Lengkap" Binding="{Binding B4R29,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" />
</DataGrid.Columns>
I can make center my text in datagrid using this style with this code above
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
But I want it in all my columns datagridtextcolumn in my datagrid.
How I can make it like style so all my datagridtextcolumn has the same centered alignment text?
Try this Style for DataGridColumnHeader:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.Resources>
You can also put him in ColumnHeaderStyle:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
If you have the current Style for DataGridColumnHeader, then you need to use the style inheritance using BasedOn like this:
<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" <--- Here may also be the key of your Style
TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
If you want set content of DataGridCell to center, then use this Style:
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Remove Border of WPF DataGrid Header column

My requirement is that my WPF DataGrid rows should have border but the datagrid header column should not have any border.
If I put DataGrid inside Border element, then entire grid, including header column, gets the border.
I have set GridLinesVisibility="Horizontal".
Is there any way to have border only for grid rows?
Thanks!
Edit
Here is the xaml code I am using..
<Style x:Key="DataGridColumnHeaderHiddenStyle" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Height" Value="15" />
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
<StackPanel>
<ScrollViewer MaxHeight="175" VerticalScrollBarVisibility="Auto">
<DataGrid ItemsSource="{Binding SelectedItemsCollectionView}"
CellStyle="{StaticResource CellStyle}" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="{StaticResource GridBorderBrush}" ClipToBounds="True" AllowDrop="True"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
x:Name="selectedItemsGrid">
<DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="AllowDrop" Value="True" />
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="HeadersVisibility" Value="Column" />
<Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGridColumnHeaderHiddenStyle}" />
</Style>
</DataGrid.Style>
</DataGrid>
</ScrollViewer>
</StackPanel>
you can set a style for the DataGridColumnHeader and set SeparatorBrush to transparent. This seems to remove column separators also:
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="SeparatorBrush" Value="Transparent" />
</Style>

getting datagridrow of datagrid in WPF

I used this line in code but it does not give row object, row is null.
DataGridRow row =
(DataGridRow)dtgSensorReadingList.ItemContainerGenerator.ContainerFromItem(channelGrid.Items[i]);
Datagrid Xaml code:
<DataGrid Visibility="Hidden" VirtualizingStackPanel.VirtualizationMode="Standard"
CanUserAddRows="False" ColumnHeaderHeight="32" MinColumnWidth="65"
HorizontalGridLinesBrush="DarkKhaki" VerticalGridLinesBrush="DarkKhaki"
BorderBrush="DarkKhaki" Block.TextAlignment="Center" AutoGenerateColumns="True"
CanUserResizeColumns="False" CanUserReorderColumns="False" HorizontalAlignment="Left"
Margin="63,540,0,0" Name="dtgSensorReadingList" ItemsSource="{Binding}" Grid.Row="1"
Height="Auto" VerticalAlignment="Top" MaxWidth="1920" Width="Auto">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="SteelBlue" />
<Setter Property="Foreground" Value="white" />
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Background" Value="Transparent" />
</Style>
<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>
</DataGrid.Resources>
</DataGrid>
Please let me know how can I get the DatGridRow ?
if you want the selected row you can bind to SelectedItem in your datagrid.
please tell us what you wanna achieve.

Resources