Howard Bash
asked on
WPF Listbox Selection
I have a WPF application with a listbox. I click a button, add several items to the list and then subsequenly click an item, get back the row number and do some other calcs with that number.
For some reason, when I click the listbox item, it doesn't unselect a previous item and further sometimes highlights several rows in the listbox.
For some reason, when I click the listbox item, it doesn't unselect a previous item and further sometimes highlights several rows in the listbox.
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = (ListBox)sender;
int n = lb.SelectedIndex;
//some stuff not related to listbox follows
}
Make sure that your listbox has the SelectionMode property set to Single.
ASKER
It is set to Single.
I second wdosanjos.
Can you post XAML to show how ListBox is configured?
Can you post XAML to show how ListBox is configured?
ASKER
Here is the entire page of xaml
<Window x:Class="WpfBOLViewer.Main Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="627" Width="852" Loaded="Window_Loaded" WindowStartupLocation="Cen terScreen" WindowStyle="SingleBorderW indow" SizeToContent="Manual">
<Grid Height="599">
<TextBox Margin="152,12,12,63" Name="txtResults" VerticalScrollBarVisibilit y="Auto" />
<Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,27" Name="cmdLoad" VerticalAlignment="Bottom" Width="124" Click="cmdLoad_Click">Load </Button>
<ListBox HorizontalAlignment="Left" Margin="12,12,0,63" Name="listBox1" Width="125" SelectionChanged="listBox1 _Selection Changed" SelectionMode="Single" />
</Grid>
</Window>
<Window x:Class="WpfBOLViewer.Main
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="627" Width="852" Loaded="Window_Loaded" WindowStartupLocation="Cen
<Grid Height="599">
<TextBox Margin="152,12,12,63" Name="txtResults" VerticalScrollBarVisibilit
<Button Height="23" HorizontalAlignment="Left"
<ListBox HorizontalAlignment="Left"
</Grid>
</Window>
Hmmm. XAML looks ok!
1. Check/post code in "listBox1_SelectionChanged "? I doubt there could some code in execution of SelectionChanged that is selecting a item from the listbox.
2. Try to test in chunks. Like create hard-coded items in the listbox rather than populating it on button click, do not put "listBox1_SelectionChanged " event, and test the behavior. If this works then remove hard-coded listbox items and now populate it on button click and test. If this works too then connect "listBox1_SelectionChanged " event and test.
I don't see any other reason why this should not work!
1. Check/post code in "listBox1_SelectionChanged
2. Try to test in chunks. Like create hard-coded items in the listbox rather than populating it on button click, do not put "listBox1_SelectionChanged
I don't see any other reason why this should not work!
ASKER
private void listBox1_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
ListBox lb = (ListBox)sender;
int n = lb.SelectedIndex;
if (n > -1)
{
My250Record mr = ListOfProcessed250Records[ n];
string sAcc = string.Empty;
string sValue = string.Empty;
string sNameValue = string.Empty;
sValue = mr.RecType + mr.DateStamp + mr.RecordBody + mr.CRLF;
foreach (DataGrp_RecordDefs.Record Layout.Rec ordColumn rc in currentRecordSchema)
{
if (rc.ExcludeRecordFromRepor t)
{
}
else
{
sNameValue = SubStringFromChars(sValue. ToCharArra y(), rc.FieldStart - 1, rc.FieldEnd - 1);
sAcc = sAcc + rc.FieldName + ": " + sNameValue + "\r\n";
}
}
this.txtResults.Text = sAcc;
}
}
{
ListBox lb = (ListBox)sender;
int n = lb.SelectedIndex;
if (n > -1)
{
My250Record mr = ListOfProcessed250Records[
string sAcc = string.Empty;
string sValue = string.Empty;
string sNameValue = string.Empty;
sValue = mr.RecType + mr.DateStamp + mr.RecordBody + mr.CRLF;
foreach (DataGrp_RecordDefs.Record
{
if (rc.ExcludeRecordFromRepor
{
}
else
{
sNameValue = SubStringFromChars(sValue.
sAcc = sAcc + rc.FieldName + ": " + sNameValue + "\r\n";
}
}
this.txtResults.Text = sAcc;
}
}
That tosses my first doubt away! SelectionChanged look fine too.
Can you try #2? Can you also show how do you populate the listbox?
Can you try #2? Can you also show how do you populate the listbox?
ASKER
private List<MyListboxItem> m_ListBoxItems;
private void InitListBox()
{
int nCnt = ListOfProcessed250Records. Count();
string ni = string.Empty;
My250Record mr = new My250Record();
MyListboxItem mli = new MyListboxItem();
listBox1.Items.Clear();
txtResults.Text = "";
m_ListBoxItems = new List<MyListboxItem>();
for (int nlp = 0; nlp < nCnt; nlp++)
{
mr = ListOfProcessed250Records[ nlp];
//************************ ********** ********** ********** ********** ********
listBox1.Items.Add(mr.EMP_ SSN + " : " + mr.RecType);
//************************ ********** ********** ********** ********** ********
mli.Value = mr.EMP_SSN + " : " + mr.RecType;
m_ListBoxItems.Add(mli);
mr = new My250Record();
mli = new MyListboxItem();
}
this.Title = "Main Window (" + Convert.ToString(nCnt) + " translactions loaded)";
}
private void InitListBox()
{
int nCnt = ListOfProcessed250Records.
string ni = string.Empty;
My250Record mr = new My250Record();
MyListboxItem mli = new MyListboxItem();
listBox1.Items.Clear();
txtResults.Text = "";
m_ListBoxItems = new List<MyListboxItem>();
for (int nlp = 0; nlp < nCnt; nlp++)
{
mr = ListOfProcessed250Records[
//************************
listBox1.Items.Add(mr.EMP_
//************************
mli.Value = mr.EMP_SSN + " : " + mr.RecType;
m_ListBoxItems.Add(mli);
mr = new My250Record();
mli = new MyListboxItem();
}
this.Title = "Main Window (" + Convert.ToString(nCnt) + " translactions loaded)";
}
I am curious to see your running application. There does not seem to be anything apparent that can cause such behavior. Code that populates ListBox
as well as SelectionChanged are straightforward.
I think only idea I have left with is to debug and verify whether SelectionMode property is not changed at run-time. You can set-up a break-point in
"listBox1_SelectionChanged " event handler and check the value of this property at run-time.
If this still does not give any clues then I throw in the towel!
as well as SelectionChanged are straightforward.
I think only idea I have left with is to debug and verify whether SelectionMode property is not changed at run-time. You can set-up a break-point in
"listBox1_SelectionChanged
If this still does not give any clues then I throw in the towel!
ASKER
I ran with breakpoint in the SelectionChanged event. The mode of the listbox is still single. Do we have a bug in WPF here? What I am doing is really simple stuff.
ASKER
I just found this url/solution and am checking it out.
http://stackoverflow.com/questions/3843784/listbox-is-selecting-many-items-even-in-selectionmode-single
http://stackoverflow.com/questions/3843784/listbox-is-selecting-many-items-even-in-selectionmode-single
ASKER
This "fix" worked. It appears that there are some optimizations with string storage in .NET and this is the side effect. The solution requires you wrap the strings in a simple class so that they are handled uniquely.
Interesting, but should be something you can turn on/off.
Interesting, but should be something you can turn on/off.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A collaborative effort. Finally found the solution