Reputation: 89
I have tried removing the styles, and making columns not ReadOnly, with no difference. No matter what I change, the text in the cells will only align Left or Center UNLESS the text is formatted as currency, all the fields are strings so I don't understand why that would have an effect, but it does. The amount column contains $##.## and it works, number columns, ######, should align right also but they won't.
If I add "<Setter Property="HorizontalAlignment" Value="Right"/>" to the existing CellStyle, all the columns EXCEPT the number columns align right. It is baffling.
<DataGrid x:Name="CustomerGrid" Grid.Row="2" Grid.ColumnSpan="3" Margin="5,10,5,5"
ItemsSource="{Binding Customers}" SelectedItem="{Binding SelectedCustomer}"
Height="Auto" Width="Auto" SelectionMode="Single" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeColumns="True"
SelectionUnit="FullRow" Background="Transparent" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserSortColumns="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" GridLinesVisibility="All"
VerticalAlignment="Stretch" AlternationCount="2">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell" >
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="Yellow"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}"/>
<Setter Property="Height" Value="25"/>
<Setter Property="FontSize" Value="12"/>
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="WhiteSmoke"/>
<Setter Property="FontWeight" Value="Normal"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightCyan"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<!--hide the default button at the beginning of each row-->
<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Width" Value="0"/>
</Style>
</DataGrid.RowHeaderStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Customer Name" MinWidth="200" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FullName}" TextAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" ToolTip="{Binding FullName}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Customer Number" MinWidth="120" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding CustomerNumber}" Margin="0,0,5,0" TextAlignment="Right" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Bill To Cust Number" MinWidth="120" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding BillToCustNumber}" Margin="0,0,5,0" TextAlignment="Right" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Payment Amount" MinWidth="150" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Amount}" HorizontalAlignment="Right" Margin="0,0,5,0" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Upvotes: 1
Views: 80
Reputation: 242
I tried using TextAlignment="Right"
for cells and it set them to the right side.
When I tried your code with minor tweaking, it worked like this:
Datagrid cells align Left & Center, but not Right.
Here is the change:
<DataGridTemplateColumn Header="Payment Amount" MinWidth="150" IsReadOnly="True">
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Right" />
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Amount}" TextAlignment="Right" Margin="0,0,5,0" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
And here is my class Customer
public class Customer
{
public string? FullName { get; set; }
public string? CustomerNumber { get; set; }
public string? BillToCustNumber { get; set; }
private decimal _amount { get; set; }
public string? Amount
{
get => _amount.ToString("$0.00");
set
{
if (decimal.TryParse(value, out decimal number))
_amount = number;
}
}
}
So, could you provide little bit more information (code with sample data) to test it? Or even show image/link to how you want it? Sorry if I misunderstood the question.
Upvotes: 0