Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit How to find checkbox controls in listbox?
8 replies. Latest Post by vitya on May 30, 2008.
(0)
littleqi...
Member
4 points
6 Posts
03-21-2008 1:19 PM |
my code:
<ListBox x:Name="lstCourse" Grid.Row="1" Grid.Column="2"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <CheckBox x:Name="chkCourse" Content="{Binding CourseName}" Margin="5" Tag="{Binding CourseId}"></CheckBox> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
i use DataTemplate to display content.how to find checkbox controls in listbox?how to use listboxitem?i use lstCourse.Items return IList interface not listboxitem class.Thanks.
behind code use listbox's itemsSource property
void soapClient_GetCoursesByProductAndTermCompleted(object sender, GetCoursesByProductAndTermCompletedEventArgs e) { if (e.Error == null) { OnlineTestService.CourseTable[] courseTables= e.Result; lstCourse.ItemsSource = courseTables; } }
sladapter
All-Star
17181 points
3,133 Posts
03-22-2008 6:12 PM |
I did not find a way to find control defined in the DataTemplate either. But if you are trying to find which one is checked there are several ways:
1) Hook a event handler to the CheckBox:
<CheckBox x:Name="chkCourse" Content="{Binding CourseName}" Margin="5" Tag="{Binding CourseId}" Click='chkCourse_Click"></CheckBox>
private void CheckBox_Click(object sender, RoutedEventArgs e) { CheckBox c = sender as CheckBox; // Here is the one that user just clicked
//do something with it. }
2) Check the data that bound to the Checkbox.IsChecked field:
In your Data Item you can add a property for your Cource object:
public bool CourseSelected {get;set;};
then bind this property to the ChechBox.IsChecked field:
<CheckBox x:Name="chkCourse" Content="{Binding CourseName}" Margin="5" Tag="{Binding CourseId}" IsChecked="Binding CourseSelected, ,Mode=TwoWay}"></CheckBox>
If the checkbox is checked, courseTables[index].IsChecked = true;
03-23-2008 10:35 AM |
Hi sladapter, thanks for your help.
i use listbox's datatemplate and add checkbox in it.i find a question again.if i am not select listbox's item,then click current checkbox which is not checked.
Allen Ch...
Star
13662 points
1,780 Posts
03-24-2008 4:39 AM |
Hi:
littleqiang520: Hi sladapter, thanks for your help. i use listbox's datatemplate and add checkbox in it.i find a question again.if i am not select listbox's item,then click current checkbox which is not checked.
Could you clarify your problem? I'm not sure what do you mean. You can tell us what's your requirement directly.
Thanks
03-24-2008 9:09 AM |
Allen,
I think I got what he means. I found the same problem with checkbox being a listItem. If you try to click the checkbox directly, it won't get checked (or unchecked). You have to somehow click the Item outline to make the item get focus first. Then you can click the checkbox. So it takes more than one click to have the checkbox checked.
David Anson
Participant
1718 points
212 Posts
03-24-2008 3:19 PM |
All,
This problem is ultimately due to an issue with Silverlight 2 Beta 1 where it incorrectly fires ListBox's GotFocus event twice (vs. once). That misinformation interferes with ListBox's selection logic and causes problems like this one (you can see similar behavior if you put a TextBox in a ListBoxItem).
Here's a demonstration of the problem along with a workaround for the specific scenario in this thread (just change ClickMode of the CheckBox from the default of Release to Press):
<ListBox> <TextBlock Text="click me then"/> <CheckBox Content="try to check me - bug"/> <CheckBox Content="try to check me - workaround" ClickMode="Press"/> </ListBox>
desopedr
96 points
34 Posts
03-24-2008 10:25 PM |
Hi,
I come back to the title of the topic, somebody knows how to find a control defined in the DataTemplate of a ListBox ?
Thanks in advance
sfawcett
8 points
5 Posts
03-31-2008 12:36 PM |
Allen Chen – MSFT: Hi: littleqiang520: Hi sladapter, thanks for your help. i use listbox's datatemplate and add checkbox in it.i find a question again.if i am not select listbox's item,then click current checkbox which is not checked. Could you clarify your problem? I'm not sure what do you mean. You can tell us what's your requirement directly. Thanks
His (and my problem) is he can't iterate through the list of checkboxes to see what is checked. I am getting around this by linking IsChecked to a bool in my data object (two way). Im not sure i want the data associated like this as the list is a simple lookup table whereas the data is going into a different object on save.
regards
Scott
vitya
111 points
75 Posts
05-30-2008 2:42 PM |
sfawcett:His (and my problem) is he can't iterate through the list of checkboxes to see what is checked. I am getting around this by linking IsChecked to a bool in my data object (two way). Im not sure i want the data associated like this as the list is a simple lookup table whereas the data is going into a different object on save.
Also, if it's not the clicked checkbox is what is needed for post processing, but another control's (say, textbox) value in the same row, then getting the checkbox is not enough... Is there a way to get to the controls in the template directly?
cheers