Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

Since I couldn't find anything on the forum about this problem, i'm hoping you could help me out.

I would like to show a list of checkboxes within a ListBox.

Code i'm using:
http://pastebin.com/PCizZT5w

XAML:
http://pastebin.com/8fhbQpMS

However, when I select a single checkbox, and scroll down untill the end, it randomly selects a few different items.

Anyone who had this problem before, or knows how to fix this??

Yours Sincerely,
Lars
Posted

1 solution

Wow. That's a weird issue. Even weirder dropping the code into my own project and verifying the problem!

From the random nature of the checkboxes, and the fact that when you scroll down some will be checked, and when you scroll back others will be checked, I'd say it'd have something to do with the checkboxes being created and destroyed when they are pushed on and off the screen by the scrolling motion. The Listbox only generates the ItemTemplates for the items currently on the screen (and a small buffer).

What I'd be doing instead, and what's worked for me in the past, is to create a data structure that combines the boolean 'checked' value with the integer.

So the steps are as follows:
- Make a class that has an int and a bool as two properties.
- Modify your datatemplate so that it binds the checkbox to the bool and the label to the int, as follows:

HTML
<datatemplate>
<stackpanel orientation="Horizontal">
<checkbox ischecked="{Binding theBool, Mode=TwoWay}" />
<textblock text="{Binding theInt}" />
</stackpanel>
</datatemplate>


Then to check if an item is checked or not, you can simply iterate through lstCheckbox.Items and check each object for "obj.theBool == true".

That's the best solution I can think of, hope it helps!
 
Share this answer
 
Comments
larssy1 14-Dec-11 3:25am    
And this should fix the problem of creating and destroying the object??

Since it WILL have to show the actual checks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900