Ich habe eine Auflistung einer Basisklasse
Die Klasse Basis ist abstrakt.
Die Auflistung
Die MainWindow.xaml sieht so aus:
Das UserControl UcTyp1 habe ich so aufgebaut:
Spoiler anzeigen
Und UcTyp2 so:
Spoiler anzeigen
Wie kann ich im DataTemplate des ListViews entscheiden, welches UserControl ich für das entsprechende Item verwende?
Anbei noch mal das komplette Projekt
public ObservableCollection<Basis> MyList { get; set; } = new ObservableCollection<Basis>();
und möchte diese Auflistung als ItemsSource für beispielsweise eine ListView nehmen.Die Klasse Basis ist abstrakt.
Basis
wird von Typ1
und Typ2
beerbt.Die Auflistung
MyList
initialisiere ich wie folgt in der MainWindow.xaml.cs:C#-Quellcode
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- DataContext = this;
- }
- public ObservableCollection<Basis> MyList { get; set; } = new ObservableCollection<Basis>
- {
- new Typ1 { Text = "Ich bin Typ 1", Count=10},
- new Typ2 { Text = "Ich bin Typ 2", IsEnabled = true}
- };
- }
Die MainWindow.xaml sieht so aus:
XML-Quellcode
- <Window x:Class="DynamischeUserControls.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:DynamischeUserControls"
- mc:Ignorable="d"
- d:DataContext="{d:DesignInstance Type={x:Type local:MainWindow}, IsDesignTimeCreatable=True}"
- Title="MainWindow" Height="450" Width="800">
- <ListView ItemsSource="{ Binding Path=MyList}">
- <ListView.ItemTemplate>
- <DataTemplate>
- <!--Entscheidung treffen, welches UserControl zu verwenden ist.
- UcTyp1 für Item == Typ1, bzw. UcTyp2 für Item == Typ2-->
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- </Window>
Das UserControl UcTyp1 habe ich so aufgebaut:
C#-Quellcode
- public partial class UcTyp1 : UserControl
- {
- public UcTyp1()
- {
- InitializeComponent();
- DataContext = this;
- }
- public Typ1 MyType { get => (Typ1)GetValue(MyTypeProperty); set => SetValue(MyTypeProperty, value); }
- public static readonly DependencyProperty MyTypeProperty = DependencyProperty.Register(nameof(MyType), typeof(Typ1), typeof(UcTyp1));
- }
XML-Quellcode
- <UserControl x:Class="DynamischeUserControls.UcTyp1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:local="clr-namespace:DynamischeUserControls"
- mc:Ignorable="d"
- d:DataContext="{d:DesignInstance Type={x:Type local:UcTyp1}, IsDesignTimeCreatable=True}"
- d:DesignHeight="450" d:DesignWidth="800">
- <StackPanel>
- <TextBlock Text="{Binding MyType.Text, StringFormat='{}Text: {0}'}"/>
- <TextBlock Text="{Binding MyType.Count, StringFormat='{}Count: {0}'}"/>
- <ListView ItemsSource="{Binding MyType.Items}"/>
- </StackPanel>
- </UserControl>
Und UcTyp2 so:
C#-Quellcode
- public partial class UcTyp2 : UserControl
- {
- public UcTyp2()
- {
- InitializeComponent();
- DataContext = this;
- }
- public Typ2 MyType { get => (Typ2)GetValue(MyTypeProperty); set => SetValue(MyTypeProperty, value); }
- public static readonly DependencyProperty MyTypeProperty = DependencyProperty.Register(nameof(MyType), typeof(Typ2), typeof(UcTyp2));
- }
XML-Quellcode
- <UserControl x:Class="DynamischeUserControls.UcTyp2"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:local="clr-namespace:DynamischeUserControls"
- mc:Ignorable="d"
- d:DataContext="{d:DesignInstance Type={x:Type local:UcTyp2}, IsDesignTimeCreatable=True}"
- d:DesignHeight="450" d:DesignWidth="800">
- <StackPanel>
- <TextBlock Text="{Binding MyType.Text, StringFormat='{}Text: {0}'}"/>
- <TextBlock Text="{Binding MyType.IsEnabled, StringFormat='{}IsEnabled: {0}'}"/>
- </StackPanel>
- </UserControl>
Wie kann ich im DataTemplate des ListViews entscheiden, welches UserControl ich für das entsprechende Item verwende?
Anbei noch mal das komplette Projekt