Skip to main content

Microsoft Silverlight

Answered Question How to find checkbox controls in listbox?RSS Feed

(0)

littleqiang520
littleqi...

Member

Member

4 points

6 Posts

How to find checkbox controls in listbox?

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;
            }
        }

MSN:littleqiang520@hotmail.com

sladapter
sladapter

All-Star

All-Star

17181 points

3,133 Posts

Re: How to find checkbox controls in listbox?

 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;

 

 

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

littleqiang520
littleqi...

Member

Member

4 points

6 Posts

Re: Re: How to find checkbox controls in listbox?

Hi sladapter, thanks for your help.Big Smile

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.

MSN:littleqiang520@hotmail.com

Allen Chen – MSFT
Allen Ch...

Star

Star

13662 points

1,780 Posts

Re: Re: How to find checkbox controls in listbox?

Hi:

littleqiang520:

Hi sladapter, thanks for your help.Big Smile

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

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

sladapter
sladapter

All-Star

All-Star

17181 points

3,133 Posts

Re: Re: How to find checkbox controls in listbox?

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. 


 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

David Anson
David Anson

Participant

Participant

1718 points

212 Posts

Microsoft
Answered Question

Re: Re: How to find checkbox controls in listbox?

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>


http://blogs.msdn.com/delay

This posting is provided "AS IS" with no warranties, and confers no rights.

desopedr
desopedr

Member

Member

96 points

34 Posts

Re: Re: How to find checkbox controls in listbox?

 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
sfawcett

Member

Member

8 points

5 Posts

Re: Re: How to find checkbox controls in listbox?

Allen Chen – MSFT:

Hi:

littleqiang520:

Hi sladapter, thanks for your help.Big Smile

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
vitya

Member

Member

111 points

75 Posts

Re: Re: How to find checkbox controls in listbox?

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

vitya

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities