简体   繁体   English

WinUI 3:将枚举绑定到 DataGridComboBoxColumn

[英]WinUI 3: Binding a Enum to a DataGridComboBoxColumn

I'm trying to bind an Enum to a DataGridComboBoxColumn .我正在尝试将 Enum 绑定到DataGridComboBoxColumn For this I use the EnumValuesExtensions .为此,我使用EnumValuesExtensions I use WinUI 3 and Windows App SDK in version 1.0.3.我在 1.0.3 版本中使用 WinUI 3 和 Windows App SDK。

Unfortunately, I get the following error at runtime:不幸的是,我在运行时收到以下错误:

Markup extension could not provide value.标记扩展无法提供价值。

I use the EnumValuesExtensions like this:我像这样使用EnumValuesExtensions

<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:我想将此枚举绑定到 ComboBox:

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

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

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

And finally my Observable:最后是我的 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? EnumValuesExtensions在 WinUI 3 中不起作用吗?

Please refer to this GitHub issue .请参考这个 GitHub 问题

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; }
}

Windows App SDK 1.1.1 的更新解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM