How can I get the checked checkbox values using MV

2019-06-09 04:30发布

问题:

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.

回答1:

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



回答2:

A excellent way is create a new class, because when you must bind the properties into Grid, ItemsControl or other element that it use ItemsSource is easier.

In your model, the class will be...

public class ListModel: INotifyPropertyChanged
{
    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            if (isSelected != value)
            {
                isSelected = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
                }
            }
        }
    }
}

You binding this property to CheckBox

<ItemsControl  ItemsSource="{Binding ListModelBinding, Mode=TwoWay}">
     <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
</ItemsControl>

In your ViewModel

private ObservableCollection<ListModel> _listModelBinding;
public ObservableCollection<ListModel> ListModelBinding
    {
        get { return _listModelBinding; }
        set { _listModelBinding= value; RaisePropertyChanged("ListModelBinding"); }
    }    

The solution serve several items. If you only one property, simply create a bool property like I created ListModelBinding in ViewModel, and after you bind this property to CheckBox in xaml.

When you want retrieve data only apply Linq in ListModelBinding and that's it.