Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

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 :)

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 :)

Get the Monday date of a given week in a given year

A user on Silverlight forum made me to write this post.

Quoting the user question:
"I want to get first and last date of week given by a week number and year"

For example if Week Number is 26 and Year is 2009, then the output must be 29/06/2009.

However I did the first date of given week and year, here is what the logic is:

1) Get the date of the first day of the given year. That is the first date of any year would be Year + "-1-1".
2) Find out the date of the monday of this week. For example, for 2009, the first monday date would be 12-29-2008.
3) Now, when we add (number of weeks * 7) to the very first monday, we get the monday of the given week and given year.

Thats all we have to do.

Here is the sample code:

int iWeek = 26;
int iYear = 2009;
DateTime firstDayOfYear;
DateTime firstMondayOfYear;
DateTime finalDate;
//Step 1
firstDayOfYear = Convert.ToDateTime(iYear.ToString() + "-1-1");
DayOfWeek t = 1 - firstDayOfYear.DayOfWeek + 1;
int intDayOfWeek = (int)firstDayOfYear.DayOfWeek;
//Step 2
firstMondayOfYear = firstDayOfYear.AddDays(1 - intDayOfWeek).Date;
//Stpe 3
finalDate = firstMondayOfYear.AddDays((iWeek) * 7);
 
finalDate will have 6/29/2009.
 
Happy coding  :)