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
0
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
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
Hello Jon,
I wanted to follow up with a small example to clarify further, find the demo attached. Here's the relevant code:
Here's a screenshot at runtime:
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
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
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
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
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