【问题标题】:WinUI 3: Binding a Enum to a DataGridComboBoxColumnWinUI 3:将枚举绑定到 DataGridComboBoxColumn
【发布时间】:2022-06-10 19:56:46
【问题描述】:

我正在尝试将 Enum 绑定到 DataGridComboBoxColumn。为此,我使用EnumValuesExtensions。我在 1.0.3 版本中使用 WinUI 3 和 Windows App SDK。

不幸的是,我在运行时收到以下错误:

标记扩展无法提供价值。

我像这样使用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>

相关代码:

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

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

视图模型中的绑定属性:

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

我想将此枚举绑定到 ComboBox:

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

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

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

最后是我的 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
}

谁能告诉我为什么会出现上述错误? EnumValuesExtensions 在 WinUI 3 中不工作吗?

【问题讨论】:

    标签: c# xaml desktop-application winui-3


    【解决方案1】:

    请参考this GitHub issue

    作为一种解决方法,您似乎可以添加一个具有枚举类型的虚拟类:

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

    【讨论】:

      猜你喜欢
      • 2011-12-21
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      相关资源
      最近更新 更多