Home > database >  WinUI 3: Binding a Enum to a DataGridComboBoxColumn
WinUI 3: Binding a Enum to a DataGridComboBoxColumn

Time:06-22

I'm trying to bind an Enum to a DataGridComboBoxColumn. For this I use the EnumValuesExtensions. I use WinUI 3 and Windows App SDK in version 1.0.3.

Unfortunately, I get the following error at runtime:

Markup extension could not provide value.

I use the EnumValuesExtensions like this:

<Page
    x:Class="BSolutions.SHES.App.Views.BuildingStructurePage"
    ...
    xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
    xmlns:ui="using:CommunityToolkit.WinUI.UI"
    xmlns:enums="using:BSolutions.SHES.Models.Enumerations"
    ...
    >

    <Grid>
        ...
        <controls:DataGrid AutoGenerateColumns="False"
                           ItemsSource="{x:Bind ViewModel.Devices, Mode=OneWay}">
           <controls:DataGrid.Columns>
              <controls:DataGridTextColumn Binding="{Binding Name}" Header="Name" />
              <controls:DataGridComboBoxColumn Binding="{Binding Type}" 
                                               ItemsSource="{ui:EnumValues Type=enums:DeviceType}"
                                               Header="Typ" />
           </controls:DataGrid.Columns>
        </controls:DataGrid>
        ...
    </Grid>
</Page>

Associated code behind:

public sealed partial class BuildingStructurePage : Page
{
   public BuildingStructureViewModel ViewModel { get; }

   public BuildingStructurePage()
   {
      ViewModel = App.GetService<BuildingStructureViewModel>();
      this.InitializeComponent();
   }
}

Bound property in the View Model:

public ObservableCollection<ObservableDevice> Devices { get; private set; } = new ObservableCollection<ObservableDevice>();

I want to bind this enum to the ComboBox:

public enum DeviceType
{
   [Description("Unbekannt")]
   Unknown = 0,

   [Description("Analogaktor")]
   AnalogActuator = 1,

   [Description("Analogaktor")]
   BinaryInput = 2,
   
   ...
}

And finally my Observable:

public class ObservableDevice : ObservableValidator
{
   public DeviceType Type
   {
      get => ((Device)entity).Type;
      set => SetProperty(((Device)entity).Type, value, (Device)entity, (u, n) => u.Type = n);
   }

   public List<DeviceType> DeviceTypes
   {
      get => Enum.GetValues(typeof(DeviceType)).Cast<DeviceType>().ToList();
   }

   #region --- Constructors ---

   public ObservableDevice()
      : this(new Device())
   { }

   public ObservableDevice(Device device)
      : base(device)
   { }

   #endregion
}

Can someone tell me why I get the error described above? Is EnumValuesExtensions not working in WinUI 3?

CodePudding user response:

Please refer to this GitHub issue.

As a workaround, it seems that you can add a dummy class with the enum type:

[Microsoft.UI.Xaml.Data.Bindable]
public class DummyClass
{
    public DeviceType DummyShape { get; set; }
}

CodePudding user response:

An update to Windows App SDK 1.1.1 solves my problem.

  • Related