2

How can I select a CheckBox inside a ListView via code? I found some code online to check the boxes but checked property is not available in windows phone 8.1

foreach (ListViewGroup grp in listFiles.Groups)
{
    foreach (ListViewItem item in grp.Items)
    {
        if (item.Index != 0)
        {
            item.Checked = true;
        }
    }
}

the xaml code is

<ListView x:Name="ContentListView" SelectionMode="Multiple">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Foreground="Black" Text="{Binding}" FontSize="25"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The checkboxes are automatically generated checkboxes of listview

4
  • The Checked property is available on CheckBox, in this code you are however iterating ListViewItems, not CheckBoxes. Without your view (e.g. XAML) it's impossible to guess where your CheckBoxes really are. Commented May 19, 2016 at 6:10
  • Are you not following MVVM design? Commented May 19, 2016 at 6:12
  • the checkboxes are automatic added check box....i am using list view in multiselect mode.. Commented May 19, 2016 at 6:31
  • @ManfredRadlwimmer thanks for the respose,added the xaml.. Commented May 19, 2016 at 6:59

2 Answers 2

3

You should add item which you want to be checked to the ListView's SelectedItems list

foreach(var item in MyListView.Items)
{
    MyListView.SelectedItems.Add(item);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would solve this by using the MVVM pattern. With MVVM you will seperate your business models & logic from the view. When implemented correctly you can modify your data and it will automaticly update the view.

This is a link that contains a walkthrough about how to use it. It also contains an example using the checkbox.

5 Comments

thanks for the quick reply.the function that i want is that when the user click on check box 'All' rest of the checkboxes get automatically selected
Where does checkbox All comes from?Is it one of the multiselect listviewitem
Ok when you check on ALL checkbox all other checkboxes get checked right?
i want to get them checked when ALL checkbox is checked...in the itemsource of the listview i am passing a list of strings..
I would suggest converting the list of strings to an object that contains a boolean and a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.