Whitespace is not allowed after end of Markup Extension -- Silverlight Compile Time Error

Today while working on one of my Silverlight Project, I did some changes in the user control and built the Project and all of sudden I got the compile time error saying:
Whitespace is not allowed after end of Markup Extension.

I tried finding where exactly the whitespace has come. Initially I thought while giving name to any of control I gave white space in it but was wrong and then I could find what was the issue. Check the image you will understand what mistake I did and I thought of writting this blog.

Happy Coding!!!!

How to Align Text in DataGrid Cell in Xaml and in Code Behind

Hello,

While answering a query on silverlight forum, I thought of writing this article.
Quoting the question as:

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

how i can set RESULT alignment right?

Here is what you have to do, I will show you this in both ways 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 but you can change it according to your requirement.
 
Next, how to do this in Xaml Page? Its very simple, apply this style to whichever column you want, 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);

Comments are always welcome.

Happy Coding :)

ComboBox inside Silverlight DataGrid

Hello All,

How to implement ComboBox inside Silverlight DataGrid. Here I have uploaded a sample Project which would help you all.

Source Code

Happy Coding :)

How to Bind Silverlight ComboBox with Enum

Hello All,

Binding Silverlight ComboBox with Enum. Suppose we have Enum like this:

public enum Schedule
{
     Day = 1,
     Week = 7,
     Month = 30,
     Year = 365
}
 
then how are we going to bind it with ComboBox. Here is the code:

// Returns all the Enums

public static T[] GetEnumValues()
{
       var type = typeof(T);
       if (!type.IsEnum)
       throw new ArgumentException("Type '" + type.Name + "' is not an enum");
       return (from field in type.GetFields()
       where field.IsLiteral
       select (T)field.GetValue(type)).ToArray();
}
 
ComboBox cbo = sender as ComboBox;
var enums = GetEnumValues();
cbo.ItemsSource = enums;

Happy Coding :)