gpt4 book ai didi

C# WPF Datagrid 不会在数据更新时动态排序

转载 作者:行者123 更新时间:2023-12-02 04:47:11 24 4
gpt4 key购买 nike

我的数据网格遇到一些问题:

当我更新一些数据(来自模型)时,它会显示在我的 DataGrid 上,但如果单击标题对列进行排序,当我更新现有数据时,它会开始横向移动。

这是 2 1 个示例 s :

  • 如果我添加新值,它不会出现在末尾(就像我不对数据网格进行排序时那样),但它会显示在错误的位置(每次都在同一位置)。
  • 如果我更新现有值,则在需要时顺序不会更改。

我看到了多个答案,但有些人说 DataGridTextColumn 不应该成为问题... 所以我想知道这是否是因为字典的原因... 我知道新数据的第一个问题与字典有关。

对象。

    public class Player : INotifyPropertyChanged
{
private string _id;
private string _name;
private int _damage;
private int _heal;
private int _dps;
private int _hps;
private int _time = 1;

public Player(string id, string name)
{
_name = name;
_id = id;
}

public event PropertyChangedEventHandler PropertyChanged;

public string Id
{
get { return _id; }
private set
{
_id = value;
}
}

public string Name
{
get { return _name; }
private set
{
_name = value;
NotifyPropertyChanged("Name");
}
}

public int Damage
{
get { return _damage; }
set
{
_damage = value;
NotifyPropertyChanged("Damage");
Dps = _damage / _time;
}
}

public int Heal
{
get { return _heal; }
set
{
_heal = value;
NotifyPropertyChanged("Heal");
Hps = _heal / _time;
}
}

public int Dps
{
get { return _dps; }
private set
{
_dps = value;
NotifyPropertyChanged("Dps");
}
}

public int Hps
{
get {return _hps; }
private set
{
_hps = value;
NotifyPropertyChanged("Hps");
}
}

public int Time
{
get { return _time; }
set
{
_time = value;
Dps = _damage / _time;
Hps = _heal / _time;
}
}

private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}

}

可观察集合

public sealed class ShortList
{
private static readonly ShortList instance = new ShortList();
private ObservableCollection<Player> playerList = new ObservableCollection<Player>();

private ShortList()
{
}

public static ShortList getShortList
{
get
{
return instance;
}
}

public ObservableCollection<Player> getPlayerList
{
get
{
return playerList;
}
}

public void updatePlayer(string id, string name, int damage, int heal, int time)
{
Player player;
player = findPlayer(id);
if (player != null)
{
player.Damage = player.Damage + damage;
player.Heal = player.Heal + heal;
player.Time = player.Time + time;
}
else
{
player = new Player(id, name);
player.Damage = damage;
player.Heal = heal;
player.Time = time;
playerList.Add(player);
}
}

public void clear()
{
playerList.Clear();
}

private Player findPlayer(string id)
{
foreach (Player p in playerList)
{
if (p.Id == id)
{
return p;
}
}
return null;
}

}

XAML

<DataGrid AutoGenerateColumns="False"Name="playerDataGrid" IsReadOnly="True" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Nom" Binding="{Binding Name}" MinWidth="35"/>
<DataGridTextColumn Header="Degats" Binding="{Binding Damage}" MinWidth="45" />
<DataGridTextColumn Header="DPS" Binding="{Binding Dps}" MinWidth="29" />
<DataGridTextColumn Header="Soins" Binding="{Binding Heal}" MinWidth="35" />
<DataGridTextColumn Header="HPS" Binding="{Binding Hps}" MinWidth="29" />
</DataGrid.Columns>
</DataGrid>

窗口后面的代码

public partial class MiniParser : Window
{
public MiniParser()
{
InitializeComponent();
playerDataGrid.ItemsSource = ShortList.getShortList.getPlayerList;
temporyFill();
}

private void temporyFill()
{
ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 2);
ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 0);
ShortList.getShortList.updatePlayer("123", "ABC", 50, 0, 1);
ShortList.getShortList.updatePlayer("234", "ABC", 0, 50, 1);
ShortList.getShortList.updatePlayer("345", "BCD", 1000, 25, 25);
ShortList.getShortList.updatePlayer("456", "CDE", 250, 0, 25);
}

private void startMI_Click(object sender, RoutedEventArgs e)
{
ShortList.getShortList.updatePlayer("5678", "BABA", 100, 100, 100);
ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 0);
}
}

当然,后面的大部分代码都是出于测试目的...但想法是模型正在更新, View 需要反射(reflect)更改(甚至排序)。

最佳答案

您没有筹集PropertyChanged属性更新时发生的事件。这意味着一个新项目具有 name更改为"""Test"或更改的现有项目不会引发 PropertyChanged 事件来让 UI 知道值已更改且需要更新。

要修复此问题,请让您的属性在更改时引发 PropertyChanged 事件。

public class Player : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
RaisePropertyChanged("Name");
}
}
}

另外,我正在重新阅读您的问题,但我不知道您在做什么 ObservableDictionary ,但我建议更改它,以便您的 DataGrid 直接绑定(bind)到 ObservableCollection<Player> 。我怀疑这是你的问题的一部分。

var playerCollection = new ObservableCollection<Player>();
playerCollection.Add(new Player { Name = "Test 1" });
playerCollection.Add(new Player { Name = "Test 2" });
playerCollection.Add(new Player { Name = "Test 3" });

playerDataGrid.ItemsSource = playerCollection;

关于C# WPF Datagrid 不会在数据更新时动态排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com