Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Conditional Controls at Runtime in Silverlight DataGrid

Rate me:
Please Sign up or sign in to vote.
4.93/5 (5 votes)
9 Aug 2011CPOL4 min read 32.9K   744   17  
Conditional controls at Runtime in Silverlight DataGrid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace GridInSilverlight
{
    public partial class MainPage : UserControl
    {


        public MainPage()
        {
            InitializeComponent();

            btnLoad_Click(this, new RoutedEventArgs());
        }



        private ObservableCollection<Customer> GetCustomers()
        {


            ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
            Customer c = new Customer() { Name= "John Buchanan",Place= "Austrelia",Phone= "+55 965323326",IsCorporate= false,CustomerMood= Mood.Normal };
            customers.Add(c);

            Customer c1 = new Customer() { Name = "MarcsLab", Place = "Japan", Phone = "+05 965323326", IsCorporate = true, CustomerMood = Mood.Satisfied };
            customers.Add(c1);

            Customer c2 = new Customer() { Name = "Johnathan", Place = "USA", Phone = "+01 3652212", IsCorporate = false, CustomerMood = Mood.UnSatisfied };
            customers.Add(c2);

            Customer c3 = new Customer() { Name = "Aatish Sethi", Place = "India", Phone = "+91 36665232", IsCorporate = false, CustomerMood = Mood.NA };
            customers.Add(c3);

            Customer c4 = new Customer() { Name = "Naveen Prabhu", Place = "India", Phone = "+91 37896653", IsCorporate = false, CustomerMood = Mood.UA };
            customers.Add(c4);

            Customer c5 = new Customer() { Name = "Gautham Nayak", Place = "India", Phone = "+91 33655236", IsCorporate = false, CustomerMood = Mood.Normal };
            customers.Add(c5);

            Customer c6 = new Customer() { Name = "Dhananjaya Kumar", Place = "India", Phone = "+91 88655236", IsCorporate = false, CustomerMood = Mood.Satisfied };
            customers.Add(c6);

            Customer c7 = new Customer() { Name = "KK Kamath", Place = "India", Phone = "+91 88655236", IsCorporate = false, CustomerMood = Mood.UA };
            customers.Add(c7);

            Customer c8 = new Customer() { Name = "Vani KG", Place = "India", Phone = "+91 88655236", IsCorporate = false, CustomerMood = Mood.UnSatisfied };
            customers.Add(c8);
            return customers;
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
           
                dgCustomers.ItemsSource = GetCustomers();
         
        }

    }

    public class MoodConvertor:IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            
            Customer cusObj = value as Customer;
            Image img = new Image();
            img.HorizontalAlignment = HorizontalAlignment.Center;
            img.VerticalAlignment = VerticalAlignment.Center;
            switch (cusObj.CustomerMood)
            {

                case Mood.NA:
                    TextBlock tb = new TextBlock();
                    tb.HorizontalAlignment = HorizontalAlignment.Center;
                    tb.VerticalAlignment = VerticalAlignment.Center;
                    tb.Text  = " NA ";
                    return tb;
                case Mood.Normal:
                        
                        img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Normal.png", UriKind.Relative));
                    return img;
                    
                case Mood.Satisfied:

                    img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Satisfied.png", UriKind.Relative));
                    return img;
                 

                case Mood.UnSatisfied:
                    img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("UnSatisfied.png", UriKind.Relative));
                    return img;

                case Mood.UA:
                    HyperlinkButton btn = new HyperlinkButton();
                    btn.HorizontalAlignment = HorizontalAlignment.Center;
                    btn.VerticalAlignment = VerticalAlignment.Center;
                    btn.Content = "Invite";
                    return btn;
                   
                default :
                    TextBlock tbU = new TextBlock();
                      tbU.HorizontalAlignment = HorizontalAlignment.Center;
                    tbU.VerticalAlignment = VerticalAlignment.Center;
                    tbU.Text = "-";
                    return tbU;
            }


        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Microsoft
India India
Nothing special .. I like challenges and love my critics.

Microsoft | Bangalore | India

Blog : http://manaspatnaik.com/blog

Twitter@manas_patnaik

Comments and Discussions