xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Introduction to Data Templating Sample">
Factory
ItemTemplateSelector="{StaticResource myDataTemplateSelector}"
HorizontalContentAlignment="Stretch"
IsSynchronizedWithCurrentItem="True"/>
ContentTemplate="{StaticResource myEmployeeTemplate}"/>
//File:Window.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Data;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class Employee : INotifyPropertyChanged
{
private string name;
private string description;
private int priority;
private EmployeeType type;
public event PropertyChangedEventHandler PropertyChanged;
public Employee()
{
}
public Employee(string name, string description, int priority, EmployeeType type)
{
this.name = name;
this.description = description;
this.priority = priority;
this.type = type;
}
public override string ToString()
{
return name.ToString();
}
public string EmployeeName
{
get { return name; }
set
{
name = value;
OnPropertyChanged("EmployeeName");
}
}
public string Description
{
get { return description; }
set
{
description = value;
OnPropertyChanged("Description");
}
}
public int Priority
{
get { return priority; }
set
{
priority = value;
OnPropertyChanged("Priority");
}
}
public EmployeeType EmployeeType
{
get { return type; }
set
{
type = value;
OnPropertyChanged("EmployeeType");
}
}
protected void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
public class Employees : ObservableCollection
{
public Employees(): base()
{
Add(new Employee("A", "Cut", 2, EmployeeType.Factory));
Add(new Employee("B", "Fix", 2, EmployeeType.Factory));
Add(new Employee("C", "Email", 1, EmployeeType.Office));
Add(new Employee("D", "Read", 3, EmployeeType.Office));
Add(new Employee("E", "Clean", 1, EmployeeType.Factory));
Add(new Employee("F", "Review", 2, EmployeeType.Office));
}
}
public enum EmployeeType
{
Factory,
Office
}
public class EmployeeListDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate
SelectTemplate(object item, DependencyObject container)
{
if (item != null && item is Employee)
{
Employee taskitem = item as Employee;
Window window = Application.Current.MainWindow;
if (taskitem.Priority == 1)
return
window.FindResource("importantEmployeeTemplate") as DataTemplate;
else
return
window.FindResource("myEmployeeTemplate") as DataTemplate;
}
return null;
}
}
}