I have loaded my outlook contacts with checkbox in a datagrid and i have 100 contacts.I check all and then if i uncheck 3 contacts, while scrolling, the other contacts in between are uncheck. Can anyone guide me what was wrong?
Rows in the datagrid are virtualized. This means that rows aren't actually there until they are scrolled into view (except for the onces already in view of course). Once they get out of view controls are distroyed and once they get back into view they are
created again. This means any state that is only stored by the control, in this case the CheckBox.IsChecked property, is lost.
To preserve state you should databind it to the underlying model. A simple way might be to derive a class from your Contact class and include an IsSelected property of type bool. Then you can bind your CheckBox to the IsSelected property and it will keep
state.
HTH.
-------------
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com
laaki
Member
8 Points
67 Posts
Checkbox
Oct 27, 2009 08:17 AM | LINK
I have loaded my outlook contacts with checkbox in a datagrid and i have 100 contacts.I check all and then if i uncheck 3 contacts, while scrolling, the other contacts in between are uncheck. Can anyone guide me what was wrong?
laaki
mrjvdveen
Contributor
2285 Points
426 Posts
Re: Checkbox
Oct 27, 2009 08:38 AM | LINK
Rows in the datagrid are virtualized. This means that rows aren't actually there until they are scrolled into view (except for the onces already in view of course). Once they get out of view controls are distroyed and once they get back into view they are created again. This means any state that is only stored by the control, in this case the CheckBox.IsChecked property, is lost.
To preserve state you should databind it to the underlying model. A simple way might be to derive a class from your Contact class and include an IsSelected property of type bool. Then you can bind your CheckBox to the IsSelected property and it will keep state.
HTH.
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com
laaki
Member
8 Points
67 Posts
Re: Re: Checkbox
Oct 27, 2009 09:45 AM | LINK
I have given like this is this correct?
<
CheckBox Margin="1,7,0,0" x:Name="chbx1" Tag="{Binding Path=Email}" Width="15" Height="25" IsChecked="{Binding Path=Checked}" ></CheckBox>laaki
mrjvdveen
Contributor
2285 Points
426 Posts
Re: Re: Checkbox
Oct 27, 2009 10:12 AM | LINK
You do want to specify this is a two way binding so put this in
IsChecked="{Binding Path=Checked, Mode=TwoWay}"
Other than that it's fine.
HTH.
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com
laaki
Member
8 Points
67 Posts
Checkbox
Oct 27, 2009 01:15 PM | LINK
laaki