【发布时间】:2021-02-27 10:31:08
【问题描述】:
我从包含用户数组的数据库中收到一个查询,在用户类中我有一个名为 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>
【问题讨论】:
标签: c# wpf xaml datagrid material-design-in-xaml