当复选框放在itemscontrol中并且数据绑定到可观察集合时,使用按钮选中所有复选框 [英] Check all checkboxes with a button, when the checkbox is placed inside itemscontrol and with databinding to an observable collection

查看:90
本文介绍了当复选框放在itemscontrol中并且数据绑定到可观察集合时,使用按钮选中所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个字符串和checkboexes列表。它有两个属性,一个是ItemName,一个是CheckedStatus。我希望此按钮能够检查所有复选框。



Hello,

I have a list of string and checkboexes. It has two properties one is the ItemName and one is the CheckedStatus. I want this button to be able to check all the checkboxes.

<ItemsControl Name="IC" ItemsSource="{Binding ItemsSorted}">
 <ItemsControl.ItemTemplate>
  <DataTemplate>
    <StackPanel>
      <CheckBox Name="Mycb" IsThreeState="False"

        FontSize="12" Foreground="WhiteSmoke" 

        Content="{Binding ItemName}" IsChecked="{Binding IsChecked}"/>
    </StackPanel>
   </DataTemplate>
  </ItemsControl.ItemTemplate>
 </ItemsControl>







然后我在这里的按钮位于同一个窗口但当然在ItemsControl之外






Then I have here the button in the same Window but outside the ItemsControl of course

<Button Grid.Row="1" Content="Select All" Name="SelectAll" Style="{StaticResource ButtonStyle}" MinWidth="150" Margin="0,5,20,20" Click="SelectAll_Click"/>





这是WindowAViewModel.cs





Here is the WindowAViewModel.cs

public WindowAViewModel(string pageTitle, WindowBViewModel windowBViewModel)
        {
            PageTitle = pageTitle;

            MyItems.Add(new MyCustomItemViewModel("Apple", windowBViewModel));
            MyItems.Add(new MyCustomItemViewModel("Orange", windowBViewModel));
            MyItems.Add(new MyCustomItemViewModel("Banana", windowBViewModel));
        }





这是MyCustomItemViewModel.cs





Here is MyCustomItemViewModel.cs

[ImplementPropertyChanged]
    public class MyCustomItemViewModel
    {
        public string ItemName { get; set; }
        public bool IsChecked { get; set; }
        public WindowBViewModel WindowBViewModelObj { get; set; }

        public MyCustomItemViewModel(string itemName, WindowBViewModel windowBViewModel)
        {
            ItemName = itemName;
            WindowBViewModelObj = windowBViewModel;
        }

        private void OnIsCheckedChanged()
        {
            if (IsChecked)
                WindowBViewModelObj.MyItems.Add(this);
            else
                WindowBViewModelObj.MyItems.Remove(this);
        }
    }





这里是BaseWindowViewModel.cs





and here is the BaseWindowViewModel.cs

public class BaseWindowViewModel
    {
        public string PageTitle { get; set; }

        private ObservableCollection<MyCustomItemViewModel> _myItems;
        public ObservableCollection<MyCustomItemViewModel> MyItems
        {
            get
            {
                if (_myItems == null)
                {
                    _myItems = new ObservableCollection<MyCustomItemViewModel>();
                    _myItemsSorted = CollectionViewSource.GetDefaultView(_myItems);
                    _myItemsSorted.SortDescriptions.Add(new SortDescription() { PropertyName = "ItemName" });
                }
                return _myItems;
            }
        }

        private ICollectionView _myItemsSorted;
        public ICollectionView MyItemsSorted { get { return _myItemsSorted; } }
    }





我的尝试:



我试过这个:





What I have tried:

I have tried this:

private void SelectAll_Click(object sender, RoutedEventArgs e)
  {
  foreach (var currentItem in IC.Items)
     {
     var container = IC.ItemContainerGenerator.ContainerFromItem(currentItem) as FrameworkElement;
     var checkBox = container.FindName("Mycb") as CheckBox;
         {
            checkBox.IsChecked = true;
         }
        }
       }





但抛出异常System.NullReferenceException:'对象引用没有设置为对象的实例。'



but threw an exception "System.NullReferenceException: 'Object reference not set to an instance of an object.'"

推荐答案

你得到一个空引用,因为.FindName没有找到任何东西,在使用前检查返回的值,如果回来的东西检查一下。如果没有返回任何内容,那么应该有一个更深层次的问题。
You get a Null Reference because the .FindName did not find anything, check the returned value before use, if something was returned check it. If nothing is returned and there should be one that is a deeper problem.


这篇关于当复选框放在itemscontrol中并且数据绑定到可观察集合时,使用按钮选中所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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