I don't figure out a solution for a clean and resuable code for bind a enum to a combobox in WinUi 3.
I have a general class that holds all the enum of my projects and in that I have this code:
public class Enums
{
public enum ViewMode
{
[Display(Name = "Come nel dispositivo")]
AsDevice,
[Display(Name = "Chiaro")]
Light,
[Display(Name = "Scuro")]
Dark
}
}
In the ViewModel file I have this code:
private IList<Enums.ViewMode> _viewsMode = Enum.GetValues(typeof(Enums.ViewMode)).Cast<Enums.ViewMode>().ToList();
public IList<Enums.ViewMode> ViewsMode => _viewsMode;
public Enums.ViewMode ViewMode
{
get { return _model.ViewMode; }
set
{
if (_model.ViewMode != value)
{
_model.ViewMode = value;
RaisePropertyChanged();
UpdateCommand.RaiseCanExecuteChanged();
}
}
}
Finally in the xaml file I have this code:
<ComboBox Width="160" ItemsSource="{x:Bind ViewModel.ViewsMode}" SelectedItem="{x:Bind ViewModel.ViewMode,Mode=TwoWay}"/>
And so far so good, it works perfectly. but it display "AsDevice" or "Light" or "Dark" while I want to display the DisplayName property as "Come nel dispositivo" or "Chiaro" or "Scuro", How can I do that? Thanks to everyone