基于按钮单击使用WPF MVVM从列表框中选择所有项目 [英] Based on button click select all the items from listbox using WPF MVVM

查看:111
本文介绍了基于按钮单击使用WPF MVVM从列表框中选择所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i我正在尝试使用Wpf MVVM实现从Listbox中选择的SelectAll / UnSelectAll和多个项目。但是我在ViewModel.i中遇到问题,写下面的代码。你可以请一个人向我分享解决方案。

XAML代码

Hi All,
i am trying to implement SelectAll/UnSelectAll and Multiple items selected from Listbox using Wpf MVVM.but i am getting issue in ViewModel.i written the below code.any one could you please share the solution to me.
XAML Code

<ListBox Name="lstReferenc" ItemsSource="{Binding Granularitylist}" DisplayMemberPath="Name" 
                             SelectionMode="Extended"  Margin="103,8,9,8" SelectedValuePath="Name"  >
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ListBox}">
                                <Setter Property="IsSelected" Value="{Binding IsSelected}" />
                            </Style>
                        </ListBox.ItemContainerStyle></ListBox><pre>
In View Model Code is 
 public ICommand SelectAll { get; private set; }
        public ICommand UnSelectAll { get; private set; }
public ExtractInfoVM()
        {
            try
            {
                VendorList= GetVendors();
                
                SelectAll = new DelegateCommand(SelectAllmethod);
                UnSelectAll = new DelegateCommand(UnSelectAllmethod);
            }
            catch (Exception ex)
                {
                    throw ex;
                }

        }
public void SelectAllmethod()
        {
            foreach (item i in yourCollection)
            {
                i.IsSelected = true;
            }
        }
here i am getting  compile time error cause of 'item'

What I have tried:

and i tried to implement multiple items selected from but when i debugging it's display single item only for that i am using below code 
<pre>public List<string> _SelectedGranularityItems;
        public List<string> SelectedGranularityItem
        {
            get { return _SelectedGranularityItems; }
            set { _SelectedGranularityItems = value; }
        }





任何人都可以帮助我。



Any one could you please help to me.

推荐答案

Quote:

我写了下面的代码。

foreach (item i in yourCollection)
{
    i.IsSelected = true;
}



你显然没有写那段代码。您在某处 * 发现了一些伪代码,并将其复制到您的项目中,而无需了解它正在做什么,或者您需要哪些部分替换。



您需要将 item 替换为集合中项目的类型,或者 var 关键字。你还没有告诉我们你收藏中物品的类型名称,所以你必须自己动手工作。



你需要更换 yourCollection ,其中包含存储集合的属性或字段的名称。猜测,这可能是 Granularitylist VendorList - 你在你的问题中提到了两个,并且不清楚哪个你试图使用的收藏品。



无论你的收藏品是什么类型,你都需要确保它有公共 IsSelected 布尔属性。您还需要修复 ItemContainerStyle ,目前定位 ListBox 而不是 ListBoxItem



我建议你回到你找到代码的地方,重新阅读整个帖子,并尝试理解它。不要只是随意地将代码块复制到你的项目而不理解它们的作用!




*猜测,您复制了此StackOverflow答案中的代码[ ^ ]。


You clearly didn't write that code. You found some pseudo-code posted somewhere *, and copied it to your project without bothering to understand what it's doing, or what parts you need to replace.

You need to replace item with the type of the items in your collection, or with the var keyword. You haven't told us the type name of the items in your collection, so you'll have to work that one out for yourself.

You need to replace yourCollection with the name of the property or field which stores your collection. At a guess, that would be either Granularitylist or VendorList - you mentioned both in your question, and it's not clear which collection you're trying to use.

Whatever type is in your collection, you'll need to make sure it has a public IsSelected boolean property. You'll also need to fix your ItemContainerStyle, which is currently targeting ListBox instead of ListBoxItem.

I suggest you go back to where you found the code, re-read the entire post, and try to understand it. Don't just randomly copy blocks of code to your project without understanding what they do!


* At a guess, you copied the code from this StackOverflow answer[^].


这篇关于基于按钮单击使用WPF MVVM从列表框中选择所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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