Selection bug for listbox where the list items are value types / structs and contain duplicates?

前端 未结 4 1961
耶瑟儿~
耶瑟儿~ 2020-12-19 16:04

I turned an Horizontal ItemsControl to a Listbox so that I am able to select individual items but found that the selection was broken. Took some time to distill out the prob

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 16:49

    Why not simply use a better collection class as your datasource to overcome the problem

    var collection = new[]
     {
         new Book {Id = 1, Name = "Book1"},
         new Book {Id = 2, Name = "Book2"},
         new Book {Id = 3, Name = "Book3"},
         new Book {Id = 4, Name = "Book4"},
         new Book {Id = 3, Name = "Book3"},
     };
     var Books = collection.ToDictionary(b => Guid.NewGuid(), b => b);
     DataContext = Books;
    

    And this will be your DataTemplate

    
      
        
          
        
      
    

提交回复
热议问题