.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > How to select or UnSelect all checkboxes in a ListView
 

How to select or UnSelect all checkboxes in a ListView

Hi All,

I have a listview and I am binding this ListView using an XML file. The first column of the ListView is CheckBox. I am using Two converters to set the checkbox properties isChecked and isEnabled to set the values. I have a checkbox in the header too corresponding to my first column (Checkbox column). On Click of my CheckBox in the header, I need to check all enabled checkboxes in the ListView as well as on unckeck of header checkbox should be unchecked all enabled checkboxes in the ListView. Please find below code snippet

XAML Part,

<

Window x:Class="ListViewWithControls.ListViewWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:ValueConverters"

Title="Window1" Height="306" Width="450">

<Window.Resources>

<local:ConvertStringToBool x:Key="boolConv"/>

<local:CheckBoxEnableDisable x:Key="enableConv"/>

</Window.Resources>

<Grid>

<ListView Name="lvPlace" ItemsSource="{Binding Source={StaticResource PlaceList}, XPath=Rows/Row}" FontFamily="Calibri" FontSize="14" Foreground="Black" >

<ListView.View>

<GridView>

<GridViewColumn Width="85">

<GridViewColumn.HeaderTemplate>

<DataTemplate>

<CheckBox Name="chkStatusHeader" Checked="selectAll" Unchecked="unSelectAll"></CheckBox>

</DataTemplate>

</GridViewColumn.HeaderTemplate>

<GridViewColumn.CellTemplate>

<DataTemplate>

<CheckBox Name="chkStatus" IsChecked="{Binding XPath=Cell1,Converter={StaticResource boolConv},Mode=OneWay}" IsEnabled="{Binding XPath=Cell1,Converter={StaticResource enableConv},Mode=OneWay}"></CheckBox>

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

<GridViewColumn>

<GridViewColumn.Header>

<TextBlock Text="Place" Style="{StaticResource ColHeaderText}" />

</GridViewColumn.Header>

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBlock Name="tnLinkText" Text="{Binding XPath=Cell2}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

</GridView>

</ListView.View>

</ListView>

</Grid>

</

Window>

Please find below my converters

namespace

ValueConverters

{

[

ValueConversion(typeof(string), typeof(bool))]

class ConvertStringToBool:IValueConverter

{

#region

IValueConverter Members

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

string data = value.ToString();

if (data == "Approved")

{

return true;

}

return false;

}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

//throw new NotImplementedException();

return true;

}

#endregion

}

[

ValueConversion(typeof(string), typeof(bool))]

class CheckBoxEnableDisable : IValueConverter

{

#region

IValueConverter Members

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

string data = value.ToString();

if (data == "Registered")

{

return true;

}

return false;

}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

//throw new NotImplementedException();

return true;

}

#endregion

}

}

The following methods I have written in the code behind to handle check/Uncheck all check boxes....

private void selectAll(object sender, RoutedEventArgs e)

{

for (int i = 0; i < Place.Items.Count; i++)

{

}

}

private void unSelectAll(object sender, RoutedEventArgs e)

{

for (int i = 0; i < Place.Items.Count; i++)

{

}

}

Could you plz someone help me out how can solve my issue.

I would definitely appreciate you..

Thanks & Regards,
Faisal. N

Faisal Noordeen
Using the MVVM pattern, I would solve this problem by loading the xml into a collection in the view model, and then bind the ListView to that collection. The objects in the collection would each have a boolean property for the checkbox. I would add two commands: CheckAllCommand and UnCheckAllCommand. The commands would only have to iterate through the collection in the model view and change the boolean properties. This way the UI just has to do some simple bindings and the view model can do the work.
Graeme.Hill
Hi,

Thanks for the reply.

The data which binding to ListView coming from Exterenal source (Third party server). I don't have control to change while checking the checkbox. The only thing I can do that Once checked click on Update I can send information that to be updated. Based on the Update the ListView will be reflected with new values.

Can somebody help me out as I am very new to WPF and spending last two days for the solution... Really hopeless...

Regards,
Faisal. N
Faisal Noordeen
Hello Faisal,


Sorry I am not sure I caught the scenario. If there is any misunderstanding, please let me know.

From the given code the UI check box is bound to datasouce instance by one way mode.

It seems that you don't want to passthe UI changes to the datasource directly.

As your words : I don't have control to change while checking the checkbox

Based on my understanding,

you can create ahelper class containing the source from the third party.

Add an addintional bool property to thehelper class to sync with the CheckBox' IsChecked property.

Wedon't access the third party source directly.

It should be the ideafrom Graeme.

Thanks.
Please mark the replies as answers if they help and unmark them if they provide no help
Hua Chen
Hi All,

Thanks for the effort put forwarded to answer my question. I will give more input to the scenario here...

I provided the sample in my question is exactly matching with our requirement....

We are directly binding ListView with XML data...XML contains each row with cell values...
First column is some status string values...e.g stage 1, stage2, stage3 etc...
We need to change this as bool value to bind checkbox properties ( Both ischecked and isEnabled)
If the value is stage1 the checkbox should be unchecked and disabled
If the value is stage2 the checkbox should be unchecked and enabled
If the value is stage3 the checkbox should be checked and disabled

we are handling this by using two converters and binding to Ischecked and IsEnabled properties of a checkbox.

In our screen the intention of the user is to find all stage2 status(unchecked and enabled) entries and make it to stage 3 status(checked and disabled) by Onclick of a button MakeStage3 ( This should only work on click of MakeStage3 button).

If I didn't set Mode=OneWay as soon as user check any checkbox in the listview it wil automatically will be changed to checked and disabled . SO I changed the mode to OneWay.

The only way I am looking for loop through all the enabled checkboxes in the ListView(Status column)and set ischecked value to true (In the Checked event handler of the checkbox in the Header template) . Do the Reverse in the UnChecked event handler of the checkbox in the Header.

Does this Possible? if not, Please guide me in the right direction

I will definitely appreciate you.

Thanks & Regards,
Faisal
Faisal Noordeen

You can use google to search for other answers

Custom Search

More Threads

• Change an event within the storyboard
• ListView StyleSelector only Styling What is Visible
• Popup, Mouse, StaysOpen, Focus, IsOpen and binding
• ScrollViewer
• Changing Controls in a Datatemplate with parameters
• Adorners and AddIn controls
• Usercontrol Nightmare
• Access to pixels in MediaElement?
• SQL: Login failed for user '(null)'
• Rendering text in 3D / Viewport3D