Search code examples
c#windows-phone-8windows-phone-8.1

Select Checkbox inside a ListView programmatically


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


Solution

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