This is a migrated thread and some comments may be shown as answers.

Horizontal Aligmnent of Cell contents

4 Answers 73 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 11 Apr 2018, 03:46 AM

I'm using a DataGrid and I can right align a header by creating a style for target type "DataGridColumnHeader", and setting HorizontalAlignment to right. 

I can't figure out how to right align the cell contents for that column though. I've tried setting HorizontalContentAlignment on the "DataGridColumnHeader" style but that doesn't do anything.

 

What can I do to make the contents of a particular column right-justified?

4 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 12 Apr 2018, 08:03 PM
Hi Jon,

The header style is separate from the cell's styling, you can use the column's CellContentStyle or the CellContentStyleSelector instead. 

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Lance | Manager Technical Support
Telerik team
answered on 12 Apr 2018, 09:10 PM
Hello Jon,

I wanted to follow up with a small example to clarify further, find the demo attached. Here's the relevant code:

<grid:RadDataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="False" >
    <grid:RadDataGrid.Columns>
        <grid:DataGridTextColumn PropertyName="Name">
            <grid:DataGridTextColumn.CellContentStyle>
                <Style TargetType="TextBlock">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                </Style>
            </grid:DataGridTextColumn.CellContentStyle>
        </grid:DataGridTextColumn>
    </grid:RadDataGrid.Columns>
</grid:RadDataGrid>


Here's a screenshot at runtime:



Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Jon
Top achievements
Rank 1
answered on 12 Apr 2018, 09:39 PM
Thanks Lance. In my case my columns are being autogenerated. Is there a way to set the style for all numeric columns in xaml, or set it in the code behind?
0
Lance | Manager Technical Support
Telerik team
answered on 12 Apr 2018, 10:20 PM
Hi Jon,

There's no AutoGeneratingColumns event, however there is a Command that gives you the opportunity to create the column: GenerateColumn Command.

Once you have an instance of the column, you can set the style however you prefer. In this example, I reference an existing style that lives in App.xaml


public class CustomGenerateColumnCommand : DataGridCommand
{
    public CustomGenerateColumnCommand()
    {
        this.Id = CommandId.GenerateColumn;
    }
 
    public override bool CanExecute(object parameter)
    {
        return true;
    }
 
    public override void Execute(object parameter)
    {
        var context = parameter as GenerateColumnContext;
             
        if (context?.PropertyName == "Name")
        {
            var column = new DataGridNumericalColumn();
 
            if (Application.Current.Resources["CenteredTextBlockStyle"] is Style centeredTextBlockStyle)
            {
                column.CellContentStyle = centeredTextBlockStyle;
            }
 
            context.Result = column;
        }
    }
}


Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
DataGrid
Asked by
Jon
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Jon
Top achievements
Rank 1
Share this question
or