xem xét như sau:ObservableCollection.Contains() không hoạt động đúng
class Bind
{
public string x { get; set; }
public string y { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<Bind> cX = new ObservableCollection<Bind>();
ObservableCollection<Bind> cY = new ObservableCollection<Bind>();
cX.Add(new Bind { x = "a", y = "1" });
cX.Add(new Bind { x = "b", y = "2" });
cY.Add(new Bind { x = "a", y = "1" });
foreach (var i in cX)
{
if (!cY.Contains(i)) { lv.Items.Add(i); } //lv is a ListView control
}
}
}
Tại sao nó thêm x = "a", y = "1"
đến ListView
?
Nếu tôi thay đổi ObservableCollection
thành List
hoặc Collection
, thì cũng vậy.
Tôi có thể lặp qua tất cả các giá trị và kiểm tra xem chúng có bình đẳng không. Nhưng tôi không phải là lập trình viên C#, vì vậy có thể có một cách dễ dàng hơn. –