使用整数字段和枚举作为显示名称来填充 DataGridComboBoxColumn

Populate DataGridComboBoxColumn using integer fields and an enum as it's display name

我从包含用户数组的数据库收到查询,在用户 class 中我有一个名为 Role 的字节字段。我希望我的 DataGridComboBoxColumn 有 2 个项目。如果 Role == 0"Member",如果 Role == 1"Moderator"

User.cs

public enum UserRole
{
    Member,
    Moderator
}

public class User
{
    [JsonConstructor]
    public User(int userId, string email, string password, string token, string nickname, byte role, uint coins, int power1, int power2, int power3, int power4, DateTime createTime, DateTime? lastLoginTime)
    {
        this.UserId = userId;
        this.Email = email;
        this.Password = password;
        this.Token = token;
        this.Nickname = nickname;
        this.Role = role;
        this.Coins = coins;
        this.Power1 = power1;
        this.Power2 = power2;
        this.Power3 = power3;
        this.Power4 = power4;
        this.CreateTime = createTime;
        this.LastLoginTime = lastLoginTime;
        this.UserRole = (UserRole)role;
    }

    [JsonPropertyName("userId")]
    public int UserId { get; set; }

    [JsonPropertyName("email")]
    public string Email { get; set; }

    [JsonPropertyName("password")]
    public string Password { get; set; }

    [JsonPropertyName("token")]
    public string Token { get; set; }

    [JsonPropertyName("nickname")]
    public string Nickname { get; set; }

    [JsonPropertyName("role")]
    public byte Role { get; set; }

    [JsonPropertyName("coins")]
    public uint Coins { get; set; }

    [JsonPropertyName("power1")]
    public int Power1 { get; set; }

    [JsonPropertyName("power2")]
    public int Power2 { get; set; }

    [JsonPropertyName("power3")]
    public int Power3 { get; set; }

    [JsonPropertyName("power4")]
    public int Power4 { get; set; }

    [JsonPropertyName("createTime")]
    public DateTime CreateTime { get; set; }

    [JsonPropertyName("lastLoginTime")]
    public DateTime? LastLoginTime { get; set; }

    [JsonIgnore]
    public UserRole UserRole { get; set; }
}

MainWindow.xaml

<materialDesign:DataGridComboBoxColumn
  Header="Role"
  Width="100">
    <materialDesign:DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox" BasedOn="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type ComboBox}, ResourceId=MaterialDataGridComboBoxColumnEditingStyle}}" >
            <Setter Property="IsEditable" Value="True" />
            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Role}"/>
            <Setter Property="DisplayMemberPath" Value="UserRole"/>
        </Style>
    </materialDesign:DataGridComboBoxColumn.EditingElementStyle
</materialDesign:DataGridComboBoxColumn>

因为你已经有一个从数据库中转换字节值的枚举,你只需要一个专门的转换:

public class UserRoleToStringConverter : IValueConverter
{
    /// <summary>
    /// Converts a value.
    /// </summary>
    /// <param name="value">The value produced by the binding source.</param>
    /// <param name="targetType">The type of the binding target property.</param>
    /// <param name="parameter">The converter parameter to use.</param>
    /// <param name="culture">The culture to use in the converter.</param>
    /// <returns>
    /// A converted value. If the method returns null, the valid null value is used.
    /// </returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var enumValue = UserRole.Member;

        if (value is UserRole)
        {
            enumValue = (UserRole)value;
        }

        switch (enumValue)
        {
            case UserRole.Member: return "Member";
            case UserRole.Role: return "Moderator";
        }
    }

    /// <summary>
    /// Converts a value.
    /// </summary>
    /// <param name="value">The value that is produced by the binding target.</param>
    /// <param name="targetType">The type to convert to.</param>
    /// <param name="parameter">The converter parameter to use.</param>
    /// <param name="culture">The culture to use in the converter.</param>
    /// <returns>
    /// A converted value. If the method returns null, the valid null value is used.
    /// </returns>
    /// <exception cref="NotImplementedException"></exception>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

请注意,如果需要,您可以通过查看 CultureInfo 转换器参数来本地化您的字符串。

另一种方法可能是绑定 ViewModel 的 string 属性,以便 XAML 并在 ViewModel 中添加字符串转换。

您的代码中存在多个问题,XAML 您应该解决。

  • 数据网格列不是可视化树的一部分,这意味着相对源和元素名称绑定(如您的绑定 DataContext.Role 将不起作用。
  • 您的 UserRole 类型是 enum,但您想在组合框列中显示其常量名称作为 select 的项目。 ItemsSource 属性 期望枚举不是的可枚举类型。
  • 我假设您将 User 的数组或集合绑定到数据网格。这意味着您列的数据上下文是一个 User 实例。在这种情况下,您的组合框 ItemsSource 绑定没有意义,因为您试图绑定数据网格数据上下文,它不包含当前用户,即使包含,Role 属性 是一个 byte,不是可枚举的。
  • 您将 DisplayMemberPath 设置为 UserRole,但它不起作用,因为您想将组合框绑定到 UserRole 枚举的常量,该枚举不包含 属性 UserRole.
  • 您的 User 类型包含 byte 类型的 Role 以及 UserRole 类型的 UserRole。不清楚您要使用哪个 属性 以及另一个的用途。
  • 您的枚举类型为 underlying type int, which is the default。如果无论如何在内部使用值 byte,您可以考虑将其更改为 byte.
  • 只设置编辑样式,不设置非编辑样式,进入编辑模式时弹出,退出编辑模式时隐藏。
  • 您设置了 DisplayMemberPath,但我认为您真正想要做的是将 Role 属性 绑定为 selected 项。

在下文中,我假设您想要使用 byte 类型的 Role 属性 进行绑定,并且您想要显示 UserRole 在您的组合框列中枚举。

首先,您必须创建一个可枚举的枚举常量。您可以在代码中提供一个集合,您可以绑定到该集合,但由于它永远不会改变并且当前仅在数据网格中使用,因此您可以 create it entirely in XAML using an ObjectDataProvider 像这样:

<ObjectDataProvider x:Key="UserRoles" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
   <ObjectDataProvider.MethodParameters>
      <x:Type TypeName="local:UserRole"/>
   </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

接下来,我们需要创建一个转换器,将 byte 转换为 UserRole 并返回。

public class UserRoleToEnumConverter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {
      return (UserRole)System.Convert.ToByte(value);
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
      return (byte)(int)value;
   }
}

现在让我们将它们放在一个数据网格中并绑定到 Role 属性.

<DataGrid ItemsSource="{Binding Users}" AutoGenerateColumns="False">
   <DataGrid.Resources>
      <local:UserRoleToEnumConverter x:Key="UserRoleToEnumConverter"/>
      <ObjectDataProvider x:Key="UserRoles" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
         <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:UserRole"/>
         </ObjectDataProvider.MethodParameters>
      </ObjectDataProvider>
   </DataGrid.Resources>
   <DataGrid.Columns>
      <materialDesign:DataGridComboBoxColumn Header="Role"
                                             IsEditable="True"
                                             Width="100">
         <materialDesign:DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox" BasedOn="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type ComboBox}, ResourceId=MaterialDataGridComboBoxColumnEditingStyle}}" >
               <Setter Property="IsEditable" Value="True" />
               <Setter Property="ItemsSource" Value="{Binding Source={StaticResource UserRoles}}"/>
               <Setter Property="SelectedItem" Value="{Binding Role, Converter={StaticResource UserRoleToEnumConverter}}"/>
            </Style>
         </materialDesign:DataGridComboBoxColumn.EditingElementStyle>
      </materialDesign:DataGridComboBoxColumn>
   </DataGrid.Columns>
</DataGrid>

正如我上面已经提到的,由于您只调整编辑风格,您的专栏可能不会像您预期的那样运行。我建议您根本不要编辑样式,而是在列上设置绑定:

<materialDesign:DataGridComboBoxColumn Header="Role"
                                       IsEditable="True"
                                       Width="100"
                                       ItemsSource="{Binding Source={StaticResource UserRoles}}"
                                       SelectedItemBinding="{Binding Role, Converter={StaticResource UserRoleToEnumConverter}}"/>

这种方法可以满足您的要求,但使用枚举常量直接显示 UI 文本是不好的。考虑本地化和具有多个作品的项目。您不会显示驼峰式文本。展望未来,也许您应该考虑使用更灵活、更强大的解决方案。