public class PropertyValueSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (item != null && item is PropertyItem) { PropertyItem propItem = item as PropertyItem; if (propItem.Value is PropertyValueString) { return (container as ContentPresenter).FindResource("DataTemplateText") as DataTemplate; } else if (propItem.Value is PropertyValueInt) { return (container as ContentPresenter).FindResource("DataTemplateText") as DataTemplate; } else if (propItem.Value is PropertyValueChoice) { return (container as ContentPresenter).FindResource("DataTemplateCombobox") as DataTemplate; } else if (propItem.Value is PropertyValueBoolean) { return (container as ContentPresenter).FindResource("DataTemplateCheckbox") as DataTemplate; } else { return (container as ContentPresenter).FindResource("DataTemplateText") as DataTemplate; } } else { return (container as ContentPresenter).FindResource("DataTemplateText") as DataTemplate; } } }
public class PropertyItem { public string Name { get; set; } public PropertyValue Value { get; set; } } public interface PropertyValue { } public class PropertyValueString : PropertyValue { private string value; public string Value { get { return this.value; } set { this.value = value; } } } public class PropertyValueInt : PropertyValue { public int Value { get; set; } } public class PropertyValueChoice : PropertyValue { public string ChoicesString { get; set; } public string[] Choices { get { return (ChoicesString ?? "").Split(','); } } public string Value { get; set; } }
listView1.ItemsSource = new[] { new PropertyItem {Name="property1", Value=new PropertyValueString {Value="propValue1"}}, new PropertyItem {Name="property2", Value=new PropertyValueChoice {ChoicesString="value1,value2,value3", Value="value2"}}, new PropertyItem {Name="property3", Value=new PropertyValueBoolean {Value=true}} };