if (this.chkSelectAll.IsChecked == true)
            {
                foreach (hl_whs_move_getCmdByTypeResult item in g.Items)
                {
                   
                }
            }
            else
            {
                g.SelectedItems.Clear();
            }全消可以,但全选好像不行哦...
各位高手帮忙看看..

解决方案 »

  1.   

    hl_whs_move_getCmdByTypeResult 只是一个ItemsSource,listview的SelectionMode 为:Multiple
      

  2.   

    foreach (ListViewItem item in listView1.Items)
                    item.Selected = true;
      

  3.   


    foreach (ListViewItem  item in g.Items)
                    {
                        item.Selected = true;
                    }报错哦:
    Error 109 The event 'System.Windows.Controls.ListBoxItem.Selected' can only appear on the left hand side of += or -= D:\2dwms\WMS\Operation\CmdDist.xaml.cs 313 26 WMS
      

  4.   

    3楼没错,楼主是不是把哪个EventHandler当成item.Selected属性了
      

  5.   


     foreach (ListViewItem item in g.Items)
                    {
                        item.IsSelected = true;
                    }
    如果设成IsSelected 则不会报说语法错..因为我的数据来源是LINQ里的存储过程来的,
     List<hl_whs_move_getCmdByTypeResult> q = cmd.hl_whs_move_getCmdByType(0).ToList();
                                               
              this.goodList4.DataContext = q;但会出现异常:Unable to cast object of type 'Avanti.dal.wmsDal.hl_whs_move_getCmdByTypeResult' to type 'System.Windows.Controls.ListViewItem'.
      

  6.   

    呵呵,刚看到是WPF中的ListView
    我和3楼刚才说的是WinForm中的Listview
      

  7.   


    嗯,,多谢哥们,现在的问题就是,listview的items为:hl_whs_move_getCmdByTypeResult
    没法把它转成listviewitem,所以没法遍历,
      

  8.   

     if (this.checkBox1.Checked)
                {
               foreach (ListViewItem it in this.listView1.Items)
                        it.Selected = true;
                   this.listView1.Focus();
                }
                else
                {
                    this.listView1.SelectedItems.Clear();
                }
    另设置:
      this.listView1.MultiSelect = true;
       this.listView1.HideSelection = false;
      

  9.   


    想法跟我的是一样的,但现在的问题是:
    lisviview 的datacontext为:hl_whs_move_getCmdByTypeResult类型,
    无法转换为listviewItem...
      

  10.   

    直接SelectAll不就好了吗?listView1.SelectAll();
      

  11.   


    对,selectall确实是可以选中,但效果不明显哦,,,背景色没有变,给人的感觉就是不知道有没有被选..
      

  12.   

     g.SelectAll();
     g.Focus();可以实现像全部选中的效果,但总感觉有些不对,,呵呵
      

  13.   


    你说的效果有些不对指的是Focus的虚线框吧~~可以使用
    Keyboard.Focus(g.Items[0] as IInputElement); (使第一个item focus,这里可以根据需要设定不同的item得到焦点)
    代替 g.Focus();当然事先要判断一下g中有没有Item,以免抛异常
      

  14.   

     private UIElement GetListViewCellControl(int rowIndex, int cellIndex, ListView ltv)//获取ListView中的控件
            {
                //首先应得到 ListViewItem,所有可视UI 元素都继承了UIElement
                UIElement u = ltv.ItemContainerGenerator.ContainerFromIndex(rowIndex) as UIElement;
                if (u == null) return null;            //然后在ListViewItem元素树中搜寻单元格
                while ((u = (VisualTreeHelper.GetChild(u, 0) as UIElement)) != null)
                    if (u is GridViewRowPresenter) return VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(u, cellIndex), 0) as UIElement;            return u;
            }
     private void CheckedCheckBox(ListView listView, bool bChecked)//选中或取消CheckBox
            {
                try
                {
                    for (int i = 0; i < listView.Items.Count; i++)
                    {
                        CheckBox cb = (CheckBox)this.GetListViewCellControl(i, 0, listView);
                        cb.IsChecked = bChecked;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
      

  15.   


    仁兄,你的GetListViewCellControl方法返回null;