Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have scenario like, Listview with the check boxes, 1st option will be "Select all" on 1st item checked I have to checked all the items in the list box, and on 1st item unchecked i have to unchecked all the items. and if suppose all item are selected and i unchecked any other than 1st(2nd, 3rd) then 1st item should be unchecked.

What I have tried:

C#
<Window.Resources>
        <ControlTemplate x:Key="ItemTemplate" TargetType="ListViewItem">
            <Border
		BorderThickness="{TemplateBinding Border.BorderThickness}"
		Padding="{TemplateBinding Control.Padding}"
		BorderBrush="{TemplateBinding Border.BorderBrush}"
		Background="{TemplateBinding Panel.Background}"
		SnapsToDevicePixels="True">
                <ContentPresenter
			Content="{TemplateBinding ContentControl.Content}"
			ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
			HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
			VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
			SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </Border>
        </ControlTemplate>

        <Style TargetType="ListViewItem">
            <Setter Property="Template" Value="{StaticResource ItemTemplate}" />
        </Style>

        <DataTemplate x:Key="ItemDataTemplate">
            <CheckBox
			x:Name="checkbox"
			Content="{Binding}"
			IsChecked="{Binding	RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <ListView
			x:Name="checkedListView"
			SelectionMode="Multiple"
			ItemsSource="{Binding}"
			ItemTemplate="{StaticResource ItemDataTemplate}"
			CheckBox.Unchecked="OnUncheckItem" 
            CheckBox.Checked="checkedListView_Checked"/>
        </StackPanel>
    </Grid>

C#
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ObservableCollection<string> items = new ObservableCollection<string>();
            items.Add("Item 1");
            items.Add("Item 2");
            items.Add("Item 3");
            items.Add("Item 4");
            items.Add("Item 5");
            DataContext = items;
            
        }

        private void OnUncheckItem(object sender, RoutedEventArgs e)
        {
            //How to do ????;
        }
        int count = 0;
        private void checkedListView_Checked(object sender, RoutedEventArgs e)
        {
            //Somthing i tried
            //count bcoz of collection not allow me to modify.
            string str = checkedListView.SelectedItem.ToString();
            if (str == "Item 1" && count == 0)
            {
                count++;
                checkedListView.SelectAll();
            }
        }
    }

Plz, can any one help how to do?

Thanks.
Posted
Updated 2-Feb-18 1:39am

If i understand Correctly, yo need a select all checkbox in your listview.

Below is the template and code for same.

<Grid>
		<ListView x:Name="lv">
			<ListView.View>
				<GridView x:Name="gv">
					<GridViewColumn x:Name="gridClm_SelectRow" Width="35">
						<GridViewColumn.CellTemplate>
							<DataTemplate>
								<CheckBox Name="cbSelectRow" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor,
                                         AncestorType={x:Type ListViewItem}}, Path=IsSelected}"
										  HorizontalContentAlignment="Center"  HorizontalAlignment="Center"
										  VerticalAlignment="Center" Checked="chkWspSelect_Checked"  Unchecked="chkWspSelect_Unchecked"  IsThreeState="False"/>
								 </DataTemplate>
						</GridViewColumn.CellTemplate>
						<CheckBox Margin="0" x:Name="chkSelectAll" Click="chkSelectAll_Click"/>
					</GridViewColumn>
					<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" ></GridViewColumn>
				</GridView>
			</ListView.View>
		</ListView>
	</Grid> 

 public partial class MainWindow : Window
	{
		public ObservableCollection<MyEmployeeClass> EmployeeList { get; set; }
		public MainWindow()
		{
			InitializeComponent();
			EmployeeList = new ObservableCollection<MyEmployeeClass>();
			EmployeeList.Add(new MyEmployeeClass("Parth1"));
			EmployeeList.Add(new MyEmployeeClass("Parth2"));
			EmployeeList.Add(new MyEmployeeClass("Parth3"));
			EmployeeList.Add(new MyEmployeeClass("Parth4"));
			EmployeeList.Add(new MyEmployeeClass("Parth5"));
			EmployeeList.Add(new MyEmployeeClass("Parth6"));
			EmployeeList.Add(new MyEmployeeClass("Parth7"));
			lv.ItemsSource = EmployeeList;
		}

		private void chkSelectAll_Click(object sender, RoutedEventArgs e)
		{
			if (chkSelectAll.IsChecked.Value == true)
			{
				lv.SelectAll();
			}
			else
			{
				lv.UnselectAll();
			}
		}

		private void chkWspSelect_Checked(object sender, RoutedEventArgs e)
		{
			ListBoxItem item = ItemsControl.ContainerFromElement(lv, e.OriginalSource as DependencyObject) as ListBoxItem;
			if (item != null)
			{
				item.IsSelected = true;
			}
		}

		private static bool individualChkBxUnCheckedFlag { get; set; }
		private void chkWspSelect_Unchecked(object sender, RoutedEventArgs e)
		{
			ListBoxItem item = ItemsControl.ContainerFromElement(lv, e.OriginalSource as DependencyObject) as ListBoxItem;
			if (item != null)
				item.IsSelected = false;

			individualChkBxUnCheckedFlag = true;
			CheckBox headerChk = (CheckBox)((GridView)lv.View).Columns[0].Header;
			headerChk.IsChecked = false;
		}

	}

		public class MyEmployeeClass
		{
			public string Name { get; set; }
			public MyEmployeeClass(string name)
			{
				Name = name;
			}
		} 


PS : You can move Methods in code behind to ViewModel in case of MVVM :)
 
Share this answer
 
Comments
Member 11859517 2-Feb-18 3:56am    
Heyy, Thanks for reply, Its working, but you are using listview column, and one extra checkbox, but i dont want that, see what i posted code, like I want pleas can you tell how can i do,
so mean that the first item of you list will serve as select all option ?
If not, you need an extra column for giving select All option.

In case if you need first element of List to act as Select All, you should go for Hierarchical Data Template and not list view. It will give you the result you want.


Hope this helps.

PS let me know if you need help with it. and if this solve it, don't forget to mark above as solution ;)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900