Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suspect the problem with your code is that it did not override <code>Equals</code>.</p> <p><code>ListBox</code> uses <code>Equals</code> to find an item so if the are multiple Equals that return true then it matches multiple items and just plain messes up.<br> Items in the <code>ListBox</code> must be unique based on Equals.<br> If you try and bind a <code>ListBox</code> to List Int32 or List string and repeat any of the values then it has the same problem. </p> <p>When you say complain. How does it complain?</p> <p>In this simple example below <code>ListView</code> did not break even with a change to the <code>GetHashCode</code>.</p> <p>Did you implement <code>INotifyPropertyChanged</code>? </p> <pre><code>&lt;Window x:Class="ListViewGetHashCode.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;StackPanel Grid.Row="0" Orientation="Horizontal"&gt; &lt;Button Click="Button_Click" Content="Button"/&gt; &lt;Button Click="Button_Click2" Content="Add"/&gt; &lt;Button Grid.Row="0" Click="Button_Click_Save" Content="Save"/&gt; &lt;/StackPanel&gt; &lt;ListBox Grid.Row="1" ItemsSource="{Binding BindingList}" DisplayMemberPath="ID" SelectedItem="{Binding Selected}" VirtualizingStackPanel.VirtualizationMode="Standard"/&gt; &lt;!--&lt;ListBox Grid.Row="1" x:Name="lbHash" ItemsSource="{Binding}" DisplayMemberPath="ID"/&gt;--&gt; &lt;/Grid&gt; &lt;/Window&gt; using System.ComponentModel; using System.Collections.ObjectModel; namespace ListViewGetHashCode { public partial class MainWindow : Window { ObservableCollection&lt;ListItem&gt; li = new ObservableCollection&lt;ListItem&gt;(); private Int32 saveId = 100; private Int32 tempId = -1; public MainWindow() { this.DataContext = this; for (Int32 i = 1; i &lt; saveId; i++) li.Add(new ListItem(i)); InitializeComponent(); } public ObservableCollection&lt;ListItem&gt; BindingList { get { return li; } } public ListItem Selected { get; set; } private void Button_Click(object sender, RoutedEventArgs e) { Int32 counter = 0; foreach (ListItem l in li) { l.ID = -l.ID; counter++; if (counter &gt; 100) break; } } private void Button_Click2(object sender, RoutedEventArgs e) { //li.Add(new ListItem(0)); // this is where it breaks as items were not unique li.Add(new ListItem(tempId)); tempId--; } private void Button_Click_Save(object sender, RoutedEventArgs e) { if (Selected != null &amp;&amp; Selected.ID &lt;= 0) { Selected.ID = saveId; saveId++; } } } public class ListItem : Object, INotifyPropertyChanged { private Int32 id; public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public Int32 ID { get { return (id &lt; 0) ? 0 : id; //if you want users to see 0 and not the temp id //internally much use id //return id; } set { if (id == value) return; id = value; NotifyPropertyChanged("ID"); } } public override bool Equals(object obj) { if (obj is ListItem) { ListItem comp = (ListItem)obj; return (comp.id == this.id); } else return false; } public bool Equals(ListItem comp) { return (comp.id == this.id); } public override int GetHashCode() { System.Diagnostics.Debug.WriteLine("GetHashCode " + id.ToString()); return id; //can even return 0 as the hash for negative but it will only slow //things downs //if (id &gt; 0) return id; //else return 0; } public ListItem(Int32 ID) { id = ID; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload