2

This is a part of my program I have a string from sql and a Listbox, I want to select those Items in Listbox that exist in string.

But the problem is this: just last item will be selected by my code!

In addition I use WPF .Net 4.5 and there aren't ListboxItem.selected property and also listBox1.GetItemText!

MYlistbox.SelectionMode=SelectionMode.Multiple;
foreach (var item in MYlistbox.items)
{
 If(Mystring.Contains(item.ToString()))
  {
   MYlistbox.SelectedValue=item.ToString(); 
  }
} 
1
  • I read previous questions they couldn't help me! Commented Aug 1, 2015 at 10:50

2 Answers 2

1

The item in Items collection is item data, not ListBoxItem, you should use ItemContainerGenerator to obtain the container which is ListBoxItem and use IsSelected property:

foreach (var item in MYlistbox.items){
   if(Mystring.Contains(item.ToString())) {
       var lbItem = MYlistbox.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
       if(lbItem != null){
           lbItem.IsSelected = true;
       }
   }
} 

Also note that ItemContainerGenerator works only if the item is loaded and rendered. It's a pity that ListBox also uses a VirtualizingStackPanel by default. So the code snippet works only for visible items (hidden items won't have any container rendered). If your listbox does not contains a large collection, virtualizing may not be needed, you can then disable virtualizing like this:

VirtualizingStackPanel.SetIsVirtualizing(MYlistbox, false);
MYlistbox.UpdateLayout();//this is very important

If keeping using virtualizing, I think you have to somehow bind IsSelected to your viewModel using a binding with some converter. This approach is more complicated but more friendly with MVVM (and should be done if you're familiar with mvvm).

Sign up to request clarification or add additional context in comments.

6 Comments

@user2576835 is your ListBox rendered? you can see it? This code should be executed when the listbox is already rendered.
Thanks God I fix it in another way!
Yes listbox is rendered before!
@user2576835 I remembered it wrong, the ListBox uses a VirtualizingStackPanel inside by default, you can disable virtualizing to make the code work, see my update at the end.
@user2576835 well if you set IsVirtualizing in XAML, everything is OK. However after setting it in code (as I wrote), we have to call UpdateLayout for the change to take effect. I know adding items to SelectedItems is a better choice but at least I would like to show you that the code I provided also works, in some case if you want to obtain the ListBoxItem, then that's the only solution.
|
0
for (int i=0, i< MYlistbox.Items.Count;i++)
{
   if(Mystring.Contains(MYlistbox.Items[i].ToString()))
     {
       MYlistbox.SelectedItems.Add.(MYlistbox.Items[i]);
     }
 }

2 Comments

I think your code means MYlistbox.SelectedItems.Add(...?
Please provide an explanation of your answer so that the asker and readers can understand the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.