ListBox Data Binding With Template Selector Sample
How to get this code going:
- Create a Windows Presentation Foundation project named ListBoxSample2
- Add a new class file named Computer.cs
- Add a new class file named ComputerTemplateSelector.cs
- Paste the following code into the corresponding files.
Replace the contents of Window1.xaml with this:
<Window x:Class="ListBoxSample2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="213" Width="291">
<Window.Resources>
<DataTemplate x:Key="compTemplate" x:Name="compTemplate">
<StackPanel>
<TextBlock FontSize="14" Text="{Binding Path=Name}" />
<TextBlock FontSize="10" Text="{Binding Path=Location}" />
<TextBlock FontSize="10" Text="{Binding Path=Make}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="compBrokenTemplate" x:Name="compBrokenTemplate">
<StackPanel>
<TextBlock FontSize="14" Foreground="Red" Text="{Binding Path=Name}" />
<TextBlock FontSize="10" Foreground="Red" Text="Broken" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid Name="mainGrid" Background="AntiqueWhite">
<ListBox Name="computerList" Margin="0,0,0,29" />
<Button Height="23" Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Pick me!</Button>
</Grid>
</Window>
Replace the contents of Window1.xaml.cs with this:
using System.Windows;
namespace ListBoxSample2
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
ComputerTemplateSelector templateSelector = new ComputerTemplateSelector();
templateSelector.CompTemplate = Resources["compTemplate"] as DataTemplate;
templateSelector.CompBrokenTemplate = Resources["compBrokenTemplate"] as DataTemplate;
computerList.ItemTemplateSelector = templateSelector;
computerList.ItemsSource = Computer.SampleComputers();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Computer selectedComputer = computerList.SelectedItem as Computer;
if (selectedComputer == null)
{
MessageBox.Show("Select something");
return;
}
MessageBox.Show("You have selected " + selectedComputer.Name);
}
}
}
Replace the contents of Computer.cs with this:
using System;
using System.Collections.Generic;
namespace ListBoxSample2
{
public class Computer
{
public String Name { get; set; }
public String Location { get; set; }
public String Make { get; set; }
public Boolean IsBoken { get; set; }
public Computer(String name, String location, String make)
: this(name, location, make, false)
{
}
public Computer(String name, String location, String make, Boolean isBroken)
{
Name = name;
Location = location;
Make = make;
IsBoken = isBroken;
}
public static IEnumerable<Computer> SampleComputers()
{
List<Computer> result = new List<Computer>();
result.Add(new Computer("Kitchen PC", "On the kitchen counter", "DELL AX443"));
result.Add(new Computer("NAS", "Network attached storage", "NSLU2"));
result.Add(new Computer("Media1", "Media server", "DELL VISTA"));
result.Add(new Computer("XBOX360", "Playing more", "MS", true));
result.Add(new Computer("KidPC", "Kids play stuff", "HP1"));
return result;
}
}
}
Replace the contents of ComputerTemplateSelector.cs with this:
using System.Windows;
using System.Windows.Controls;
namespace ListBoxSample2
{
public class ComputerTemplateSelector : DataTemplateSelector
{
private DataTemplate compTemplate;
private DataTemplate compBrokenTemplate;
public DataTemplate CompTemplate
{
get { return compTemplate; }
set { compTemplate = value; }
}
public DataTemplate CompBrokenTemplate
{
get { return compBrokenTemplate; }
set { compBrokenTemplate = value; }
}
public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
{
Computer computer = item as Computer;
if (computer != null && !computer.IsBoken)
{
return compTemplate;
}
else
{
return compBrokenTemplate;
}
}
}
}
Compile and run.
|