wpf 制作一个自定义Datagrid,我应该如何解决这个问题?

wgeznvg7  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(201)

目前我正在尝试创建一个包含玩家和怪物信息的表。但是我注意到我目前尝试的方式似乎有一些限制,不能满足我的需要。我希望该表看起来如下:

我已经创建了一个Player类和一个Monster类,它们包含实体的信息。我希望我的表具有以下特性:

  • 任意数量的玩家/怪物
  • 在INIT上排序,它是随机生成的(1-20)
  • 即时编辑内容(ObservableCollection〈〉?)
  • 将鼠标悬停在带下划线的文本上以显示移动中的信息(我创建了一个NameValuePair类,其中包含namevalue

我曾经试着做如下的事情,但却有这样的结果。

<Grid Grid.Row="2" Grid.Column="1">
    <DataGrid x:Name="creatureDatagrid">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="maxHP" Binding="{Binding MaxHp}"/>
            <DataGridTextColumn Header="AC" Binding="{Binding Ac}"/>
            <DataGridTextColumn Header="INIT" Binding="{Binding Dex}"/>
            <DataGridTextColumn Header="HP" Binding="{Binding Hp}"/>
            <DataGridTextColumn Header="Senses" Binding="{Binding DmgRes}"/>
            <DataGridTextColumn Header="Speed" Binding="{Binding Speed}"/>
            <DataGridTextColumn Header="Senses" Binding="{Binding Senses}"/>
            <DataGridTextColumn Header="Traits" Binding="{Binding Traits}"/>
            <DataGridTextColumn Header="Actions" Binding="{Binding Actions}"/>
            <DataGridTextColumn Header="Conditions" Binding="{Binding Conditions}"/>
        </DataGrid.Columns>
        </DataGrid>
</Grid>

正如您所看到的,我很难将数组转换为文本,更不用说将NameValuePair转换为name + hoverable value
有什么方法可以创建像示例中那样的表呢?这对WPF来说是可能的吗?还是我使用不同的前端框架更好?谢谢!
下面是关于我的代码/类的更多信息:

public partial class MainWindow : Window
        {
            private readonly MonsterService _monsterService;
            private readonly DatabaseService _databaseService;
            public MainWindow()
            {
                InitializeComponent();
                _monsterService = new MonsterService();
                _databaseService= new DatabaseService();
                foreach (var player in _databaseService.Players)
                {
                    creatureDatagrid.Items.Add(player);
                }
                _databaseService.DoStuff();
                foreach (var monster in _databaseService.ActiveMonsters)
                {
                    creatureDatagrid.Items.Add(monster);
                }
            }
        }
    
public partial class Player
    {
        public bool IsInParty { get; set; }
        public string Name { get; set; }
        public int Ac { get; set; }
        public string ArmorType { get; set; }
        public string[] Speed { get; set; }
        public int InitiativeBonus { get; set; }
        public string[] DmgVul { get; set; }
        public string[] DmgRes { get; set; }
        public string[] DmgImm { get; set; }
        public string[] CondImm { get; set; }
        public string[] Senses { get; set; }
        public string[] Languages { get; set; }
        public NameValuePair[] Conditions { get; set; }
        public int Id { get; set; }
    }
    
public partial class BaseMonster
    {
        public int BaseId { get; set; }
        public string BaseName { get; set; }
        public string Type { get; set; }
        public string Allignment { get; set; }
        public int Ac { get; set; }
        public string ArmorType { get; set; }
        public int MaxHp { get; set; }
        public string HitDice { get; set; }
        public string[] Speed { get; set; }
        public int Str { get; set; }
        public int Dex { get; set; }
        public int Con { get; set; }
        public int Int { get; set; }
        public int Wis { get; set; }
        public int Cha { get; set; }
        public string[] SavThrProf { get; set; }
        public string[] SkillProf { get; set; }
        public string[] DmgVul { get; set; }
        public string[] DmgRes { get; set; }
        public string[] DmgImm { get; set; }
        public string[] CondImm { get; set; }
        public string[] Senses { get; set; }
        public string[] Languages { get; set; }
        public string Challenge { get; set; }
        public NameValuePair[] Traits { get; set; }
        public NameValuePair[] Actions { get; set; }
        public NameValuePair[] LegendaryActions { get; set; }
        public string LairActions { get; set; }
        public string RegionalEffects { get; set; }
    }
    
public partial class Monster : BaseMonster
    {
        public Monster(BaseMonster baseObject) 
        {
            this.BaseId = baseObject.BaseId;
            this.BaseName = baseObject.BaseName;
            this.Type = baseObject.Type;
            this.Allignment = baseObject.Allignment;
            this.Ac = baseObject.Ac;
            this.ArmorType = baseObject.ArmorType;
            this.MaxHp = baseObject.MaxHp;
            this.HitDice = baseObject.HitDice;
            this.Speed = baseObject.Speed;
            this.Str = baseObject.Str;
            this.Dex = baseObject.Dex;
            this.Con = baseObject.Con;
            this.Int = baseObject.Int;
            this.Wis = baseObject.Wis;
            this.Cha = baseObject.Cha;
            this.SavThrProf = baseObject.SavThrProf;
            this.SkillProf = baseObject.SkillProf;
            this.DmgVul = baseObject.DmgVul;
            this.DmgRes = baseObject.DmgRes;
            this.DmgImm = baseObject.DmgImm;
            this.CondImm = baseObject.CondImm;
            this.Senses = baseObject.Senses;
            this.Languages = baseObject.Languages;
            this.Challenge = baseObject.Challenge;
            this.Traits = baseObject.Traits;
            this.Actions = baseObject.Actions;
            this.LegendaryActions = baseObject.LegendaryActions;
            this.LairActions = baseObject.LairActions;
            this.RegionalEffects = baseObject.RegionalEffects;
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public int Hp { get; set; }
        public List<int> Damage { get; set; }
        public bool IsOverhealed => Hp > MaxHp;
        public bool IsBloody => Hp <= MaxHp / 2.0;
        public bool IsNearDeath => Hp <= MaxHp / 4.0;
        public bool IsDead => Hp <= 0;
        public List<NameValuePair> Conditions { get; set; }
    }
n6lpvg4x

n6lpvg4x1#

您可以将另一个string属性添加到Player类中,以返回要在DataGrid中显示的实际值,而不是直接绑定到string[]属性,例如:

public string SensesDisplayValue => string.Join(",", Senses);

这实际上使Player类成为一种视图模型。如果它当前是其他类型的,您应该将其替换为视图模型,并将视图友好属性添加到该视图模型中。直接绑定到某种实体或业务对象通常不是一个好主意。
另一个选项是在视图中使用converter将数组转换为string值。

相关问题