Page view counter
How to find checkbox controls in listbox? Subscribe to this thread
Last post 05-30-2008 2:42 PM by vitya. 8 replies.
Sort Posts:
03-21-2008 1:19 PM
Idea [I]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

littleqiang520

Loading...
Joined on 11-05-2007
Posts 6
03-22-2008 6:12 PM
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

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,861
03-23-2008 10:35 AM
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

littleqiang520

Loading...
Joined on 11-05-2007
Posts 6
03-24-2008 4:39 AM
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.

Allen Chen – MSFT

Loading...
Joined on 03-16-2007
Posts 1,761
03-24-2008 9:09 AM
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

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,861
03-24-2008 3:19 PM
Marked as Answer
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.

David Anson

Loading...
Joined on 04-11-2006
Microsoft
Posts 192
03-24-2008 10:25 PM
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
 

desopedr

Loading...
Joined on 02-15-2008
Switzerland, Valais
Posts 33
03-31-2008 12:36 PM
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

sfawcett

Loading...
Joined on 03-14-2008
Posts 5
05-30-2008 2:42 PM
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

vitya

Loading...
Joined on 03-30-2008
Posts 75
Microsoft Communities