Looking for a job

November 6, 2012 Leave a comment

I am looking for a job either contract or perm in WPF, Silverlight, Windows 8, asp.net. Please contact me if you have any open positions. I am available immediately and will send my resume.


Categories: General

Using DataTemplateSelector in Metro Style App

March 3, 2012 3 comments

This is a short post on using DataTemplateSelector to show items differently in the same list.

Lets say we have

public class Customer
public string Id { get; set; }
public string Name { get; set; }

public class PreferredCustomer: Customer
public int Discount { get; set; }

and we create a list of customers like this and set the itemssource to any ItemsControl

List<customers> = new List();
customers.Add(new Customer { Id = “1″, Name = “customer..1″ });
customers.Add(new PreferredCustomer { Id = “2″, Name = “customer..2″, Discount=10 });
customers.Add(new Customer { Id = “3″, Name = “customer..3″ });
customers.Add(new PreferredCustomer { Id = “4″, Name = “customer..4″, Discount=20 });
customers.Add(new Customer { Id = “5″, Name = “customer..5″ });

list1.ItemsSource = customers;

In resources section, we create couple of datatemplates and a DataTemplateSelector which returns a datatemplate based on the item.

We bind the ItemTemplateSelector property of Listview to the one we created in resource section.

public class CustomerDataTemplateSelector : DataTemplateSelector
public DataTemplate CustTemplate { get; set; }
public DataTemplate PrefCustTemplate { get; set; }

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
Customer c = (Customer)item;
DataTemplate dt = c is PreferredCustomer ? this.PrefCustTemplate : this.CustTemplate;
return dt;


<DataTemplate x:Key=”dtCustomer”>
<TextBlock Text=”{Binding Name}” FontSize=”25″/>
<DataTemplate x:Key=”dtPrefCustomer”>
<TextBlock Foreground=”Green” Text=”{Binding Name}” FontSize=”25″ FontWeight=”Bold”/>
<local:CustomerDataTemplateSelector x:Key=”cdst” CustTemplate=”{StaticResource dtCustomer}” PrefCustTemplate=”{StaticResource dtPrefCustomer}”/>
<Grid Background=”{StaticResource ApplicationPageBackgroundBrush}”>

<ListView Width=”300″ x:Name=”list1″ FontSize=”30″ ItemTemplateSelector=”{StaticResource cdst}”/>


Here is the result.

Categories: Metro App, win8 Tags:

Customizing GridView Items in Metro App

March 1, 2012 21 comments

This is a short post on customizing height and width of the items in GridView. by default if we use a WrapGrid we get items as bunch of tiles.

We can change the height and width of the tiles by deriving from gridview and overriding PrepareContainerForItemOverride method like below. In the code I am making the tile double the width if the city is “City 4″.  Ofcourse we can add properties and not hardcode stuff like shown
public class MyGridView : GridView
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
var obj = item as Person;
var gi = element as GridViewItem;
if (obj.City == “City 4″)
gi.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, 2);
gi.Background = new SolidColorBrush(Colors.Beige);
gi.Foreground = new SolidColorBrush(Colors.Black);
gi.FontSize = 22;
base.PrepareContainerForItemOverride(gi, item);

Here is the xaml and code I used in the BlankPage .

protected override void OnNavigatedTo(NavigationEventArgs e)
Random r = new Random();
var items = Enumerable.Range(0, 50).Select(x =>
new Person { Id = x,
Name = “person..” + x.ToString(),
City = “City ” + (r.Next(0,5)).ToString() });
cvs.Source = items;


<Page.Resources>         <CollectionViewSource x:Name=”cvs”/>     </Page.Resources>

<local:MyGridView FontSize=”20″ FontFamily=”segoe ui” Margin=”100″ ItemsSource=”{Binding Source={StaticResource cvs}}” >
<StackPanel HorizontalAlignment=”Left” >
<TextBlock Text=”{Binding Id}” />
<TextBlock Text=”{Binding Name}” />
<TextBlock Text=”{Binding City}” />

<VariableSizedWrapGrid ItemHeight=”120″ ItemWidth=”120″ MaximumRowsOrColumns=”6″ Orientation=”Vertical”/>


Here is the result.

Categories: Metro App, win8 Tags:

Finding Key State using PInvoke in SL5 RC

September 19, 2011 1 comment

If we have to find the Key State of  CAPS lock,  NUM lock etc in silverlight 5 RC, we could do like this using PInvoke

Add the Declaration

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);

And to get the different Key states

bool CapsLock = (((ushort)GetKeyState(0×14)) & 0xffff) != 0;

bool NumLock = (((ushort)GetKeyState(0×90)) & 0xffff) != 0;

bool ScrollLock = (((ushort)GetKeyState(0×91)) & 0xffff) != 0;



Categories: Silverlight 5

Using PInvoke in SL5 to show MessageBox

September 1, 2011 1 comment

One of the new features in SL5 is the ability to use PInvoke. This post shows how to use PInvoke to show MesageBox with lot more options.

Start by creating a SL5 OOB application with elevated trust and add the following code

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, int options);
public enum MessageBoxOptions
Ok = 0×000000,
OkCancel = 0×000001,
AbortRetryIgnore = 0×000002,
YesNoCancel = 0×000003,
YesNo = 0×000004,
RetryCancel = 0×000005,
CancelTryContinue = 0×000006,

IconHand = 0×000010,
IconQuestion = 0×000020,
IconExclamation = 0×000030,
IconAsterisk = 0×000040,
UserIcon = 0×000080,

IconWarning = IconExclamation,
IconError = IconHand,
IconInformation = IconAsterisk,
IconStop = IconHand,

DefButton1 = 0×000000,
DefButton2 = 0×000100,
DefButton3 = 0×000200,
DefButton4 = 0×000300,

ApplicationModal = 0×000000,
SystemModal = 0×001000,
TaskModal = 0×002000,

Help = 0×004000, //Help Button
NoFocus = 0×008000,

SetForeground = 0×010000,
DefaultDesktopOnly = 0×020000,
Topmost = 0×040000,
Right = 0×080000,
RTLReading = 0×100000,

public enum MessageBoxResult : uint
Ok = 1,
Timeout = 32000

To actually see the messagebox, we could do something like

MessageBoxOptions options = MessageBoxOptions.CancelTryContinue | MessageBoxOptions.DefButton1 | MessageBoxOptions.IconQuestion;
MessageBox(IntPtr.Zero, “This is a messagebox with 3 buttons, one of them being default and a question icon”, “Caption”, (int)options);

Here is the result

Categories: Silverlight 5

Enum’s and MarkupExtension

April 27, 2011 Comments off

Here is quick sample to get all the values from an enum using MarkupExtension. This returns a Dictionary

 public class EnumValuesExtension : MarkupExtension
        public string EnumName { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
            IXamlTypeResolver xamlResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
            Type t = xamlResolver.Resolve(EnumName);
            return t.GetFields().Where(field => field.IsLiteral).ToDictionary(x => (int)x.GetValue(null), x => x.Name);

If we have a enum like below

 public enum Days
        Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri

We can use it in xaml like this. ‘local’ is the xmlns declaration pointing to the namespace

 <ComboBox Width=”150″ Height=”25″ DisplayMemberPath=”Value”
                  ItemsSource=”{local:EnumValues EnumName=’local:Days’}”/>

Categories: Silverlight 5 beta Tags:

DataGrid random behavior when scrolling

April 12, 2011 13 comments

I came across a post in the silverlight forums which shows the random behavior of the DataGrid. This happens obviously only when scrollbars are there and when we scroll. Wraping DataGridRowsPresenter in the Template of the DataGrid in a scrollviewer seems to get around this issue. As scrollviewer has some border margin etc. I styled it to remove all unnecessary stuff.

 <ScrollViewer Grid.ColumnSpan=”2″ Grid.Row=”1″ Style=”{StaticResource ScrollViewerStyle1}”>
                                        <sdk:DataGridRowsPresenter x:Name=”RowsPresenter” />

If you remove the style applied to the DataGrid in the sample you can clearly see the random behavior. You can download the sample here .

Categories: Silverlight 4

Get every new post delivered to your Inbox.