如何使用MVVM获取选中的复选框值? [英] How can I get the checked checkbox values using MVVM?

查看:74
本文介绍了如何使用MVVM获取选中的复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在处理WPF项目。



我有一个确认对话框,其中显示用户从CSV文件导入的重复字符串的列表。 。



用户可以选中要添加到列表(列表视图)中的重复字符串,然后单击确定。



问题是我不确定如何获取已选中的复选框项的值并将其添加到列表中。我如何知道已选中哪个复选框,然后将所选项目添加到列表中?



我想使用MVVM而不是后面的代码来完成此任务。我看到了一些后面使用代码的示例,但它们仍然使我感到困惑。



我想我必须使用命令,但是我不确定如何获取选定的复选框值?



这是我的尝试:



ViewModel中的代码:

 已选择专用布尔值; 
public bool IsSelected
{

get
{

return isSelected;
}

set
{

isSelected = value;
RaisePropertyChanged( IsSelected);

if(IsSelected == true)
{

foreach(confirmList.PassList中的变量项)
{

List.Add(item);
}

RaisePropertyChanged(LIST);

}

else if(IsSelected == false)
{

}
}
}

XAML:

 < Grid> 
< ListView ItemsSource = {装订清单}>
< ListView.View>
< GridView>
< GridViewColumn>
< GridViewColumn.CellTemplate>
< DataTemplate>
< CheckBox Content = {Binding} IsChecked = {Binding Path = IsSelected,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} />
< / DataTemplate>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>

< GridViewColumn DisplayMemberBinding = {绑定名称}
标头=名称>
< / GridViewColumn>
< / GridView>
< /ListView.View>
< / ListView>



不知道该怎么办



编辑:更新了xaml以反映外观。

解决方案

从定义模型类开始

 公共类ListModel 
{
public字符串数据{get;组; }
public bool IsSelected {get;组; }
}

然后将此模型的列表绑定到Listview的ItemsSource

  List< ListModel>数据=新的List< ListModel>(); 

修改复选框的绑定为

 < CheckBox Content = {Binding Data} IsChecked = {Binding Path = IsSelected,Mode = TwoWay /> 

然后,当您需要检索时,可以执行以下linq来获取所有选定的字符串

  IEnumerable< String> selectedData = data.Where(d => d.IsSelected).Select(d => d.Data); 

现在您将拥有在UI中的selectedData

Right now, I'm working on a WPF project.

I have a confirmation dialog that displays a list of repeated strings that the user imported from a CSV file.

The user can check off the repeated strings they want to add to a list (listview) and click ok.

The problem is I'm not sure how to get the value of the checkbox items that are checked off and add them to a list. How do I know what checkbox is selected and add the selected items to a list?

I want to do this task using MVVM and not code behind. I've seen a few examples using code behind and they still confuse me.

I think I have to use commands but I'm not sure still how to get the selected checkbox value?

Here's my attempt:

Code in ViewModel:

private bool isSelected;
    public bool IsSelected
    {

        get
        {

            return isSelected;
        }

        set
        {

            isSelected = value;
            RaisePropertyChanged("IsSelected");

            if (IsSelected == true)
            {

                foreach (var item in confirmationList.PassList)
                {

                    List.Add(item);
                }

                RaisePropertyChanged(LIST);

            }

            else if (IsSelected == false)
            {

            }
        }
    }

XAML:

<Grid>
<ListView ItemsSource="{Binding Inventory}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                         <CheckBox Content="{Binding}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn DisplayMemberBinding="{Binding Name}"
                            Header="Name">
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Not sure what else to do from here to get the checked off values?

EDIT: updated xaml to reflect look.

解决方案

Start by defining a model class

 public class ListModel
 {
     public string Data { get; set; }
     public bool IsSelected { get; set; }
 }

Then Bind the list of this model to the Listview's ItemsSource

List<ListModel> data = new List<ListModel>();

modify the binding of your check box to

<CheckBox Content="{Binding Data}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay"/>

then when you need to retrieve you may execute the following linq to get all selected strings

IEnumerable<String> selectedData = data.Where(d => d.IsSelected).Select(d => d.Data);

now you'll have all the data which is selected in the UI in the field selectedData

这篇关于如何使用MVVM获取选中的复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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