Did you know - You can use ItemTemplate to create bindings with visual representation

by rahul 7/9/2010 5:22:00 PM

In this post find out how easily you can use databinding to create a listbox which gives visual data representation. First up, create a class [Student.cs] like as follows… Notice the Students class below is an ObservableCollection. 

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace DataBinding
{
    public class Student
    {
        public string name { get; set; }
        public int marks { get; set; }

        public Student(string name, int marks)
        {
            this.marks = marks;
            this.name = name;
        }
    }

    public class Students : ObservableCollection<Student>
    {
        public Students()
        {
            Add(new Student("Michael", 34));
            Add(new Student("Scott", 44));
            Add(new Student("Paul", 56));
            Add(new Student("Peter", 87));
            Add(new Student("Adam", 92));
            Add(new Student("Dinesh", 76));
            Add(new Student("James", 15));
            Add(new Student("Ali", 19));
            Add(new Student("Suresh", 13));
        }
    }
}

Create another class called [MarksConverter.cs]. This is the class that will help us in converting the range of numbers to an
appropriate color code.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace DataBinding
{
    public class MarksConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, 
System.Globalization.CultureInfo culture) { int marks = int.Parse(value.ToString()); string color = string.Empty; if (marks < 30) return color = "Red"; if (marks < 50) return color = "Orange"; if (marks < 80) return color = "Yellow"; if (marks < 100) return color = "Lime"; return color; } public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }

Create a page, and add the following namespace

xmlns:data="clr-namespace:DataBinding"

Add the following resources inside the Grid in mainpage.xaml (or any other page)

        <Grid.Resources>
            <data:Students x:Key="studentdata" />
            <data:MarksConverter x:Key="color" />
        </Grid.Resources>

Add the following Listbox and you should be done…

 

        <ListBox Height="272" Margin="12,12,0,0" Name="listBox1" 
                 VerticalAlignment="Top" ItemsSource="{StaticResource studentdata}" 
HorizontalAlignment="Left" Width="204"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding name}" MinWidth="60"></TextBlock> <TextBlock Text="{Binding marks}"></TextBlock> <Rectangle Margin="10,0,0,0" Fill="{Binding marks,
Converter={StaticResource color}}" Width="{Binding marks}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

If you have followed along, you can see the output in the design pane as…

image

Hope this helps, Wave
Rahul

Quote of the day:
Free advice is worth the price. - Robert Half


blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

All Items
Sign in

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0