[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to select or UnSelect all checkboxes in a ListView

Posted on 2012-09-11
9
Medium Priority
?
7,337 Views
Last Modified: 2012-09-12
Hi,
         I have a listview  in a wpf and I am binding this ListView to a database table. The first column of the ListView is CheckBox.  I have a checkbox in the header too corresponding to my first column (Checkbox column). On Click of my CheckBox in the header, I would like to check all checkboxes in the ListView as well as on unckeck of header checkbox should be unchecked all  checkboxes in the ListView. How can I achieve this? The code I use is as below.


 <ListView x:Name="listView" Margin="12,8,233,13">
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <CheckBox Name="cbSelectAll" Content=" Select All" Checked="cbSelectAll_Checked" ></CheckBox>              
                        <GridViewColumn.CellTemplate>                    
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Name}"/>                              
                            </DataTemplate>                    
                        </GridViewColumn.CellTemplate>                     
                    </GridViewColumn>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding TableName}"/>
                 </GridView>
            </ListView.View>
        </ListView>

Open in new window


In C# I use this code

 listView.ItemsSource = (from Name in context.TableName select Name).ToList();

Open in new window


How can I achevive select all and unselect all by checking and unchecking the checkbox in the header?

Thanks
0
Comment
Question by:anjana81
  • 4
  • 4
9 Comments
 
LVL 20

Expert Comment

by:Rikin Shah
ID: 38385900
foreach (ListViewItem item in listView.Items)
{
    item.Selected = true; // false in case of unselect
}

Try with item.Checked = true; in case of checkbox selections
0
 

Author Comment

by:anjana81
ID: 38385934
For both the case I get error.

If I use
  item.Selected = true;

Error :
The event 'System.Windows.Controls.ListBoxItem.Selected' can only appear on the left hand side of += or -=      

If I use

item.Checked = true;

Error:'System.Windows.Controls.ListViewItem' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'System.Windows.Controls.ListViewItem' could be found (are you missing a using directive or an assembly reference?)
0
 
LVL 20

Expert Comment

by:Rikin Shah
ID: 38385989
You either have a ListViewItem or ListBoxItem. For both these are the appropriate foreach loops. Just check if you're using ListViewItem or ListBoxItem and apply the code.

foreach (ListViewItem item in listView.Items)
{
    item.IsSelected = true; // false in case of unselect
}

foreach (ListBoxItem item in listView.Items)
{
    item.IsSelected = true; // false in case of unselect
}
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:anjana81
ID: 38386139
I guess the problem is because of this code below. Here I bind the data from the sql using the code below.

ModelEntities1 context = new ModelEntities1();
listView.ItemsSource = (from Name in context.TableName select Name).ToList();

Here listview is converted to list and I hope  this is why I get the error. How can I solve this ?
0
 
LVL 20

Expert Comment

by:Rikin Shah
ID: 38386149
No, list is not the problem. Did you try my above solution? If yes, what is the error?
0
 

Author Comment

by:anjana81
ID: 38386167
I tried with your solution. But I get the error

Unable to cast object of type 'TEST.TableName ' to type 'System.Windows.Controls.ListViewItem'.

Unable to cast object of type 'TEST.TableName ' to type 'System.Windows.Controls.ListBoxItem'.
0
 
LVL 20

Accepted Solution

by:
Rikin Shah earned 1500 total points
ID: 38386237
Yes you're right, it is the way you are storing the valuing into the Listview. So what you need to do is first create a ListviewItem object, one of its constructors should take an array of strings (String[]) or you could use any of its overloaded constructors you like. The goal here is to get the ListViewItem into the Listview, not your transaction object.
0
 

Author Comment

by:anjana81
ID: 38386310
Thanks a lot. Can you give me an example code of how to achieve this.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 38386437
CheckBox.Checked = true

or checkboxObject.checked=true|false
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question