开发者

ListBox selector odd behavior when there are dupes

I'm working on a bigger project atm, but I made this simple example to show you what happens..

using System.Collections.Generic;
using System.Windows;
namespace txt
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
            var temp = new List<Info>();
            for (var i = 1; i <= 3; i++)
            {
                temp.Add(obsLst[0]); //I add 3 of the same item from obsLst to temp
            }
            lst.DataContext = temp; //lst = ListBox
        }
    }
    public class Info
    {
        public string name { get; set; }
    }
}

The ListBox ItemsSource is set to {Binding}..

When I start the application I get 3 txt.Info objects displayed and if I click any of them, 2 or even all of them get selected aswell. From my understanding the problem relies in the fact that the listbox selector cannot differentiate between the items and therefor doesn't know which one I selected.

Here's a picture of what it looks like..

ListBox selector odd behavior when there are dupes

I only clicked on the second txt.Info item.

I found a solution where someone said that I have to specify the DisplayMemberPath, but I can't really do that in the other project because I have a datatemplate for the object.

Any ideas on how I could fix this would be great..

Thx in advance.

EDIT 1: this works but it's not nice..

using System.Collections.Generic;
using System.Windows;

namespace txt
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
            var temp = new List<Container>();
            for (var i = 1; i <= 3; i++)
            {
                var t = new Container();
                t.obj = obsLst[0];
                temp.Add(t);
            }
            lst.DataContext = temp;
        }
    }
    public class Info
    {
        public string name { get; set; }
    }

   开发者_Go百科 public class Container
    {
        public Info obj { get; set; }
    }
}

In this case you need to set DisplayMemberPath="obj"

Assigning an ID to the object, doesn't work..

using System;
using System.Collections.Generic;
using System.Windows;

namespace txt
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var rand = new Random();
            var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
            var temp = new List<Info>();
            for (var i = 1; i <= 3; i++)
            {
                obsLst[0].id = rand.Next(10000);
                temp.Add(obsLst[0]);
            }
            lst.DataContext = temp;
        }
    }
    public class Info
    {
        public string name { get; set; }
        public int id { get; set; }
    }
}


I had this problem too a while ago, i fixed it by adding the id # to the item so they're always different.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜