12 Replies - 2622 Views - Last Post: 18 January 2012 - 10:44 AM Rate Topic: -----

#1 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

How to fix listbox selecing same values?

Posted 18 January 2012 - 09:11 AM

Hi

Got this issue, from what i seen it's well known yet i didn't found straight answer and threads i found are like from 2010, is there any simple solution? I just bind Listbox to List<string> and same strings are multiselected or not selected at all (when checking selected index). This is most annoying thing i seen so far in VS...
Is This A Good Question/Topic? 0
  • +

Replies To: How to fix listbox selecing same values?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 09:13 AM

You are going to need to better explain your issue. Can you post some code and a screenshot of what you are seeing? I've bound a list to a ListBox and I don't remember having any issues.
Was This Post Helpful? 0
  • +
  • -

#3 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 09:18 AM

I'm sorry i thought it's know.

If you bind/add , in my case strings, with same value, like 10x "hi", if you select item in Listbox it behaves weird, it's not deselecting properly and it's selecting multiple values (visually because index stays at first 'found')
Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 09:25 AM

Put a breakpoint in your code where you can examine the actual content of the list<string> you are bound to. I'm going to bet that you are adding to the List<string> somewhere in your code.

Quote

If you bind/add , in my case strings, with same value, like 10x "hi",

Well, yah... If you add it 10 times then it will exist 10 times. And depending on how you add they are all references to the very same object. So if you select the object, you select the 10 occurrences of the same object. That does make sense if they are all the same object.

The first link below shows more about breakpoints if you are unfamiliar.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

This post has been edited by tlhIn`toq: 18 January 2012 - 09:27 AM

Was This Post Helpful? 1
  • +
  • -

#5 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 09:34 AM

Yes i'm aware that i'm adding same values to my bound List<string> because entire thing is just there to provide visualization for user of (in this case) effects apply order, and via "Selected index" be able to click 'delete' button which will use Selected index and do RemoveAt in List<string>, and another List<myclass>.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 09:38 AM

So if you are aware that you are adding duplicate objects, what are you confused about?
Was This Post Helpful? 0
  • +
  • -

#7 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 09:40 AM

Why list box which suppose to select one value is spazzing out and selecting everything around. I expected same as Winforms behavior.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 09:57 AM

Well, I fired up a test app, and I am seeing the same thing. Wow... that's really weird. Never seen that before.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        List<string> list = new List<string>();
        list.Add("testing");
        list.Add("test");
        list.Add("hey");
        list.Add("testing");
        list.Add("test");
        list.Add("hey");
        list.Add("testing");
        list.Add("testing");

        listBox.ItemsSource = list;
    }
}



Attached Image

This just happens when I click around. Really weird.
Was This Post Helpful? 1
  • +
  • -

#9 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 10:05 AM

Umm...maybe I'm failing to see the problem....

Why don't you just set the ListBox.SelectionMode property to indicate that only one item should be selected?



Oh I see the problem :)
    <Grid>
        <Grid.Resources>
            <x:Array x:Key="Strings" Type="sys:String" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib">
                <sys:String>String</sys:String>
                <sys:String>Name</sys:String>
                <sys:String>Apple</sys:String>
                <sys:String>String</sys:String>
                <sys:String>Orange</sys:String>
                <sys:String>Name</sys:String>
                <sys:String>Bananna</sys:String>
                <sys:String>String</sys:String>
            </x:Array>
        </Grid.Resources>
      
        <ListBox ItemsSource="{StaticResource Strings}" />
    </Grid>



hmm...

-Frinny

This post has been edited by Frinavale: 18 January 2012 - 10:43 AM

Was This Post Helpful? 1
  • +
  • -

#10 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 10:07 AM

View PostFrinavale, on 18 January 2012 - 10:05 AM, said:

Umm...maybe I'm failing to see the problem....

Why don't you just set the ListBox.SelectionMode property to indicate that only one item should be selected?

-Frinny

It is 'single'.

In meantime i found fix. Turns out you need to create instance of that string by using it in class to create separate place in memory.
Source bottom post.
Still it's unexpected behavior in my opinion.
Was This Post Helpful? 0
  • +
  • -

#11 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 10:19 AM

I suspect that it has to have something to do with the Compare method that the ListBox is using to determine what item is selected.

Think about it...when you compare 2 strings that contain the same data, they are considered to be equal (makes sense): so the ListBox selects all Strings that contain the same data.

If strings were compared differently...like if they were compared on their memory address or some sort of unique ID it would probably work.

Hmm, I'd recommend wrapping the strings with some sort of object that can be compared properly by the ListBox (ie: compared on their memory address instead of on the data they contain).

Edit: You posted just before I could post...that answer is pretty much what I was suggesting :)

This post has been edited by Frinavale: 18 January 2012 - 11:10 AM

Was This Post Helpful? 1
  • +
  • -

#12 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 10:40 AM

Here is my solution to the problem....

I created a class called WrappedString that has 1 property: Value (the string that the class wraps).

I then created a converter to use on the list of (array of) strings that converts the list of strings into a list of WrappedStrings and back again.

Here are my two classes:
(WrappedString class)

Public Class WrappedString
    Public Property Value As String
      Public Sub New(ByVal strToWrap As String)
        Value = strToWrap
    End Sub
End Class


(Converter class)
Public Class StringConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert

        Dim wrappedStrings As New List(Of WrappedString)
        For Each s As String In value
            wrappedStrings.Add(New WrappedString(s))
        Next
        Return wrappedStrings

    End Function

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack

        Dim strings As New List(Of String)
        For Each ws As WrappedString In value
            strings.Add(ws.Value)
        Next
        Return strings.ToArray

    End Function
End Class


In my XAML I declared a static resource that is an array of strings and a static resource that is my converter. I bind the ListBox to the array of strings and provide my converter in the binding.

Like this:

    <Grid Height="300" Width="200">
        <Grid.Resources>
            <x:Array x:Key="Strings" Type="sys:String" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib">
                <sys:String>String</sys:String>
                <sys:String>Name</sys:String>
                <sys:String>Apple</sys:String>
                <sys:String>String</sys:String>
                <sys:String>Orange</sys:String>
                <sys:String>Name</sys:String>
                <sys:String>Bananna</sys:String>
                <sys:String>String</sys:String>
            </x:Array>
            <local:StringConverter x:Key="StringConverter"/>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="40" />
        </Grid.RowDefinitions>

        <ListBox x:Name="ListBoxOfStrings"
                 ItemsSource="{Binding Source={StaticResource Strings}, Converter={StaticResource StringConverter}}"
                 SelectedValuePath="Value"
                 DisplayMemberPath="Value" />

        <StackPanel Grid.Row="1" Orientation="Horizontal" >
            <TextBlock Text="Selected Value: " Foreground="Blue"/>
            <TextBlock Text="{Binding SelectedValue,ElementName=ListBoxOfStrings}" Foreground="Red"/>
        </StackPanel>
    </Grid>



The selected string value is displayed in a TextBlock under the ListBox.


-Frinny

This post has been edited by Frinavale: 18 January 2012 - 11:02 AM

Was This Post Helpful? 2
  • +
  • -

#13 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: How to fix listbox selecing same values?

Posted 18 January 2012 - 10:44 AM

Thanks for your effort :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1