I'm implementing a UserControl which should display a list of settings:
public class SettingPropertyItem {
string Name { get; }
Type ValueType { get; }
object Value { get; set; }
}
Based on each type in ValueType a different DataTemplate should be used.
To facilitate this the UserControl has the following Control with a SettingPropertyItem as its DataContext:
<UserControl x:Class="AVDump3Gui.Controls.Settings.SettingsView">
...
<ItemsControl Items="{Binding Properties}" Margin="16,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
...
<ContentControl Content="{Binding}"/>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
...
</UserControl>
Then the view where the Usercontrol is used, I've added a DataTemplate into its DataTemplates:
<sv:SettingsView.DataTemplates>
<DataTemplate DataType="{x:Type vm:SettingPropertyItem}">
...
</DataTemplate>
</sv:SettingsView.DataTemplates>
So far so good, everything works as expected. But now I'm a bit stumped as I don't know how to apply different DataTemplates based on a Property within the DataContext.
With WPF a DataTemplateSelector or Triggers seem to be the way to go (ignoring additional Frameworks), but they don't seem to exist in Avalonia. I've also tried styles but the selector doesn't seem to be able to access DataContext properties.
How can this be done?
In Avalonia
DataTemplateSelectorisn't needed because you can just implementIDataTemplateyourself and select the template there.i. e.