SpriteHand
Module Border
  The Silverlight Toolkit is Live
Module Border
Location: BlogsAndy's Blog    
Posted by: host 10/28/2008 10:29 AM
 
Just a little while ago at his PDC Keynote, ScottGu announced the Silverlight Toolkit, a collection of Silverlight controls available on Codeplex (live demo here). If you’ve used the ASP.NET AJAX Control Toolkit, then you’ve probably come to appreciate the utility of a community-driven set of controls, so it is great to see a project of similar scope crop up in the Silverlight universe. In this blog post, I’ll talk about how to get started with the Toolkit and playing with the TreeView control.
 
 

 

Adding Controls to the Toolbox

Even thought the Visual Studio designer is read-only for Silverlight, it can still be handy to drag/drop controls from the toolbox into the XAML editor. In order to include the new Silverlight Toolkit controls to your toolbox, just follow these steps:

1.       Right-click the VS toolbox and select “Add New Tab.” Name the tab “Silverlight Toolkit.”

2.       Right-click within the new tab and select “Choose Items.”

3.       In the “Choose Toolbox Items” dialog, go to the “Silverlight Components” tab.

4.       Navigate to the folder where you extracted the toolkit, and then to the Binaries subfolder. Then select the Microsoft.Windows.Controls.dll.  This assembly contains all new controls except the new Charting controls.

5.       Repeat the above step, selecting the Microsoft.Windows.Controls.DataVisualization.dll assembly. This assembly contains the new charting functionality.

TreeView Control

This control does what you might expect  - show a hierarchy of items in an expandable list. But what makes this TreeView control different than what you might be used to is the power of templating in Silverlight.

Let’s look at a very simple example. To add items to the TreeView at runtime, we can define TreeViewItem objects and give them “Header” objects for labels:

    TreeViewItem fruits = new TreeViewItem();
            fruits.Header = "Fruits";
            fruits.Items.Add("Banana");
            fruits.Items.Add("Orange");
            fruits.Items.Add("Apple");

            TreeViewItem veggies = new TreeViewItem();
            veggies.Header = "Veggies";
            veggies.Items.Add("Carrots");
            veggies.Items.Add("Celery");
            veggies.Items.Add("Lettuce");

            tvTest.Items.Add(fruits);
            tvTest.Items.Add(veggies);

 

Hierarchical Data Sources

We can also bind the TreeView to a hierarchical Data Source using the HierarchicalDataTemplate control, and the TreeView will automatically traverse the hierarchy and render each cascading node appropriately. Take, for example this class:

 public class FoodItem 
    {
        public FoodItem(string name, string url)
        {
            Name = name;
            ImageUrl = url;
        }

        public List<FoodItem> Items { get; set; }
        public string Name { get; set; }
        public string ImageUrl { get; set; }
  }

Note that the FoodItem class contains a list of more FoodItems. This allows us to define a hierarchy with FoodItems – for example, Fruits and Veggies:

List<FoodItem> categories = new List<FoodItem>();
FoodItem fruits = new FoodItem("Fruits", " Fruit_in_basket.jpg");
fruits.Items = new List<FoodItem>();
fruits.Items.Add(new FoodItem("Apple", "Red_Delicious.jpg"));
fruits.Items.Add(new FoodItem("Banana", " Banana.png"));
fruits.Items.Add(new FoodItem("Orange", "Orange.JPG"));
categories.Add(fruits); 

FoodItem veggies = new FoodItem("Veggies", "salad.jpg");
veggies.Items = new List<FoodItem>();
veggies.Items.Add(new FoodItem("Carrots", " Carrots.JPG"));
veggies.Items.Add(new FoodItem("Cucumbers", " Cucumbers.jpg"));
veggies.Items.Add(new FoodItem("Peppers", " peppers.jpg"));
categories.Add(veggies);

In order to consume the hierarchical data source, we set up a HierarchicalDataTemplate, with our preferred visual layout for each node in the TreeView. Note how we set the Binding of the HierarchicalDataTemplate to the member collection in the data source class that defines the hierarchy (in this case the generic List “Items”):

<controls:TreeView x:Name="tvFoodItems">
    <controls:TreeView.ItemTemplate>
        <controls:HierarchicalDataTemplate ItemsSource="{Binding Items}">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" HorizontalAlignment="Left" Width="30" Source="{Binding ImageUrl}" />
                <TextBlock Text="{Binding Name}" Margin="5,0,0,0" HorizontalAlignment="Left" FontSize="18" />
            StackPanel>
        controls:HierarchicalDataTemplate>
      controls:TreeView.ItemTemplate>
controls:TreeView>

We can then bind the TreeView in code to our hierarchical list:

tvFoodItems.ItemsSource = categories;

… after which we have an expandable hierarchy like so:

So Much More

I expect there to be a flood of blog posts and resources cropping up over the next week or so on the Silverlight Toolkit – and I’m very excited about the future of the toolkit. According to ScottGu’s release blog post, he says “We will continually add new controls to the control pack over the next few months (we expect to ultimately have more than 100 controls total).   Wow! That’s a lot of controls, and given what’s in this initial release, I look forward to seeing what great things the team comes up with!

 

Permalink |  Trackback

Comments (4)   Add Comment
Re: The Silverlight Toolkit is Live    By Anonymous on 10/28/2008 2:08 PM
Great! :) Are you at the PDC?

Re: The Silverlight Toolkit is Live    By Anonymous on 10/28/2008 7:41 PM
Nope, no PDC for me :(

But I can't complain too much, I made it to MIX and TechEd this year :)

Re: The Silverlight Toolkit is Live    By Anonymous on 10/28/2008 10:33 PM
how about TreeList? treeview + listview hybrid?

Re: The Silverlight Toolkit is Live    By Anonymous on 12/30/2008 12:18 PM
How to bind for the parent node one object and for the child node other object?


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border

RSS

Module Border Module Border
Module Border
  Diversions
Module Border

DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

INFO AND CODE



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2009 andy.beaulieu.com - Login