WPF and ObservableCollection <T>
I have an ObservableCollection<IRuleCondition> that I want to display - the IRuleCondition interface IRuleCondition used by two different classes that I want to display, RuleCondition , which simply displays one rule condition (information such as priority, property to check, etc.) and RuleConditionGroup , which may contain 2 or more RuleConditions , are grouped so that any of the conditions can match, or all, etc.
In XAML, I was wondering if there is a way to show another ListView.ItemTemplate depending on what type it encounters in the ObservableCollection<IRuleCondition> ? Or would I need to implement two different ObservableCollection s?
Here is a simple example of how this works.
So objects are defined
public interface Person { string Name { get; set; } } public class Manager : Person { public string Name { get; set; } } public class Employee : Person { public string Name { get; set; } public string ManagerName { get;set;} } This is MainWindow code for
public partial class MainWindow : Window { ObservableCollection<Person> mPeople = new ObservableCollection<Person>(); public ObservableCollection<Person> People { get { return mPeople; } } public MainWindow() { DataContext = this; mPeople.Add( new Employee{ Name = "x" , ManagerName = "foo"}); mPeople.Add( new Manager { Name = "y"}); InitializeComponent(); } } This is MainWindow XAML
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <DataTemplate DataType="{x:Type my:Employee}"> <StackPanel Background="Green" Width="300"> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding ManagerName}" /> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type my:Manager}"> <StackPanel Background="Red" Width="300"> <TextBlock Text="{Binding Name}" /> </StackPanel> </DataTemplate> </Window.Resources> <Grid> <ListBox ItemsSource="{Binding People}"></ListBox> </Grid> </Window> As you can see, there are two datasets for the manager and one for Employee

And so the crappy conclusion looks. Pay attention to the green and red background and the additional field displayed for the employee compared to the manager
1) Create two different data templates, as you said. 2) Create your own DataTemplateSelector to select the appropriate template.
One of your comments says you get an error message from your DataTemplateSelector. Make sure you implement the class correctly, maybe insert your implementation. It should be quite small and simple.