How to fix listbox selecing same values?
Page 1 of 112 Replies - 1790 Views - Last Post: 18 January 2012 - 10:44 AM
#1
How to fix listbox selecing same values?
Posted 18 January 2012 - 09:11 AM
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...
Replies To: How to fix listbox selecing same values?
#2
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 09:13 AM
#3
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 09:18 AM
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')
#4
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 09:25 AM
Quote
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
This post has been edited by tlhIn`toq: 18 January 2012 - 09:27 AM
#5
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 09:34 AM
#6
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 09:38 AM
#7
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 09:40 AM
#8
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 09:57 AM
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;
}
}

This just happens when I click around. Really weird.
#9
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 10:05 AM
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
#10
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 10:07 AM
Frinavale, on 18 January 2012 - 10:05 AM, said:
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.
#11
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 10:19 AM
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
#12
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 10:40 AM
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
#13
Re: How to fix listbox selecing same values?
Posted 18 January 2012 - 10:44 AM
|
|

New Topic/Question
Reply




MultiQuote




|