2011-12-02 67 views
2

我遇到一些麻烦,我的DataGrid:C#WPF DataGrid不动态排序上的数据更新

当我更新一些数据(从模型),它显示在我的DataGrid,但如果点击页眉当我更新现有数据时,对列进行排序时,它开始横向移动。

这里的 1例 小号

  • 如果我添加了新的价值,它不会在年底出现(像它的时候我不排序数据网格),但它显示在错误的地方(每次都是一样的地方)。
  • 如果我更新一个现有的值,那么当它需要时,顺序不会改变。

我见过多次的答案,但有人说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)); 
    } 

} 

的ObservableCollection

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);    
     } 
} 

当然后面的代码最落后的代码是有测试目的......但这个想法是模型正在更新,视图需要反映更改(即使是排序)。

回答

3

我找到了解决办法,到目前为止好:

的问题是,当你在一个对象,它已经在你的ObservableCollection改变一个字段的值的ObservableCollection不会触发事件“CollectionChanged”。 (可能因为你不改变对它的引用)。

下面是一个例子:

ObservableCollection<MyObject> myCollection = new ObservableCollection<MyObject>(); 
myCollection.add("CollectionChanged","Yes"); 
//Let's say I had a method to find a object with the first string and to change the second string 
myCollection.change("CollectionChanged", "No"); 

正如您可以猜到,当我改变了我的现有对象的字段的CollectionChanged没有触发第二部分...

所以我实施的解决方案如下:

class ObsCollection<T> : ObservableCollection<T> 
    { 
     public void UpdateCollection() 
     { 
      OnCollectionChanged(new NotifyCollectionChangedEventArgs(
          NotifyCollectionChangedAction.Reset)); 
     } 
    } 

所有你所要做的就是创建新的类型的集合,然后当你更改现有对象的字段值,调用UpdateCollection()会见HOD。

该解决方案是由吴雪松。

我想说雷切尔也是一个很大的帮助。

+1

的情况下根据MSDN,复位手段发现“大大改变了集合的内容。”是否有一种更细化的方式让排序工作?对于小型收藏品,重置似乎没问题,但对于频繁更新的大型收藏品可能会造成问题。 – KornMuffin

+0

如果将其从ObservableCollection更改为BindingList,该怎么办?我想BindingList会在单个项目更改时引发收集已更改的事件。 – Cesar

3

当属性更新时,您不会提升PropertyChanged事件。这意味着name""更改为​​的新项目或已更改的现有项目不会引发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直接绑定到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; 
+0

我觉得我做你的要求(见第一哨的变化),但遗憾的是没有解决该问题的第二部分。感谢你的帮助到目前为止,我希望我能自己找到它,但我会继续寻找我的救赎。 – Tooc

+0

@ user1078049我相信'NotifiyPropertyChanged'区分大小写,因此请尝试调整您的调用以使用正确的大小写。 'NotifyPropertyChanged(“Name”);' – Rachel

+0

谢谢,但并没有改变任何东西...... – Tooc

0

移动任何代码:

this.NotifyPropertyChanged("somepropertyname"); 

到您的属性setter方法。这是制定者在那里的原因。

此外,我第二个答案suggestig你使用ObservableCollection<T>而不是你的ObservableDictionary<TKey,TValue>。它们对于WPF绑定非常常见。