Hi friends, in this short article I will tell you about How to Align Text in DataGrid Cell in Xaml and in Code Behind with Silverlight 6. First, take a look the following code:

DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = "Result";
textColumn.Binding = new Binding("Result");
dataGrid1.Columns.Add(textColumn);

How I will set RESULT alignment right? Here is what you've got to try to to, I'll show you this in each ways that like doing it in Xaml Page and doing it in code behind. First add the style in your User control Resource:
<UserControl.Resources>
      <Style x:Key="AlignRight" TargetType="Data:DataGridCell">
          <Setter Property="HorizontalContentAlignment" Value="Right" />
     </Style>
</UserControl.Resources>

I'm doing this for Right Align however you'll be able to change it in keeping with your requirement. Next, the way to do this in Xaml Page? Its terribly straightforward, apply this style to whichever column you would like, like this:

<Data:DataGridTextColumn Header="Amount" Width="90" Binding="{Binding Amount}" IsReadOnly="True" CellStyle="{StaticResource AlignRight}"></Data:DataGridTextColumn>

Now how to do this in code behind:
DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = "Result";
textColumn.Binding = new Binding("Result");
textColumn.CellStyle = Resources["AlignRight"] as Style;
dataGrid1.Columns.Add(textColumn);

Hope it works for you!

Silverlight 6 with Free ASP.NET Hosting

Try our Silverlight 6 with Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc. You will not be charged a cent for trying our service for the next 3 days. Once your trial period is complete, you decide whether you'd like to continue.