Dynamic Icon switching depend on Enums #21436
-
|
Hello Community, I want to show an Icon based on a binded enum value. Let's say I have a class called "Club" containing an enum of types. public class Club
{
public enum ClubType
{
Unknown,
Football,
Cycling,
Swimming,
Dancing
}
public ClubType Type { get; set; } = ClubType.Unknown;
public string Name { get; set; } = "Unknown";
}I have a ListBox displaying instances of Club. My goal is to show an Icon left to the Club.Name based on the enum. My first idea was to extend my Club class of an IconPath and BackgroundColor Property. But I think this will violate the MVVM pattern so I struggling how to achieve this inside the axaml. I think use a Converter is the way to do it but I don't know how to use a EnumToBoolConverter. I even don't know which namespace i have to import into the axaml. Have a great day |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
There are at least 3 ways you could do what you want to do in the view. Using the enum to bool converter or your own custom converter as you mentioned or using a EnumToBoolCoverterOnly documented in where it is used as part of the colour picker controls <UserControl
<!-- Rest ommited for brevity -->
xmlns:converters="using:Avalonia.Controls.Converters">
<UserControl.Resources>
<converters:EnumToBoolConverter x:Key="enumConverter" />
</UserControl.Resources>
<!-- Show the football image if it is selected -->
<Icon IsVisible="{Binding Type, Converter={StaticResource enumConverter}, ConverterParameter={x:Static ClubEnum:Club.Football}}"/>
</UserControl>Custom Converternamespace MyConverters;
public static ConverterFuncs
{
public static FuncValueConverter<Club, string> ClubConverter => new((club) =>
{
return club switch
{
Club.Football => "Football",
// Rest of the options
}
});
}<UserControl
<!-- Rest ommited for brevity -->
xmlns:converters="using:MyConverters">
<!-- Show the football image if it is selected -->
<Icon Type="{Binding Type, Converter={x:Static converters:ConverterFuncs.ClubConverter}}"/>
</UserControl>DataTemplatenamespace MyTemplates;
public class ClubTemplate : IDataTemplate
{
public Control Build(object param)
{
return club switch
{
Club.Football => new Icon() { Type = "Football"},
// Rest of the options
}
}
public bool Match(object data)
{
return data is Club;
}
}<UserControl
<!-- Rest ommited for brevity -->
xmlns:templates="using:MyTemplates">
<!-- Show the football image if it is selected -->
<ContentControl Content="{Binding Club}">
<!-- This could also be put into UserControl.DataTemplates instead -->
<ContentControl.DataTemplates>
<templates:ClubTemplate/>
</ContentControl.DataTemplates>
</ContentControl>
</UserControl>You could also make your own control for displaying the club type but that is probably more complicated than you actually need for what you are doing. |
Beta Was this translation helpful? Give feedback.
There are at least 3 ways you could do what you want to do in the view.
Using the enum to bool converter or your own custom converter as you mentioned or using a
DataTemplate.EnumToBoolCoverter
Only documented in where it is used as part of the colour picker controls