根据第1列表视图项检查并取消选中所有列表视图项。 [英] Checked and unchecked all listview item based on 1st listview item.

查看:77
本文介绍了根据第1列表视图项检查并取消选中所有列表视图项。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有这样的情景,Listview带有复选框,第一个选项将是全选,第一项检查我必须检查所有列表框中的项目,并取消选中第一项,我必须取消选中所有项目。并且如果假设所有项目都被选中并且我取消选中除第1(第2,第3)以外的任何项目,则应取消选中第1项。



我尝试过:



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:

<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>




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,任何人都可以帮忙怎么做?



谢谢。


Plz, can any one help how to do?

Thanks.

推荐答案

如果我理解正确,你需要在列表视图中选择全部复选框。



以下是相同的模板和代码。



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:在MVVM的情况下,您可以将代码中的方法移到ViewModel :)



PS : You can move Methods in code behind to ViewModel in case of MVVM :)


这意味着你列表的第一项将作为全选选项?

如果没有,你需要一个额外的列来给出选择全部选项。



如果你需要List的第一个元素作为Select All,你应该选择Hierarchical Data Template而不是列表视图。它会给你你想要的结果。





希望这会有所帮助。



PS如果您需要帮助,请告诉我。如果这解决了,请不要忘记将上面标记为解决方案;)
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 ;)


这篇关于根据第1列表视图项检查并取消选中所有列表视图项。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆