简体   繁体   中英

C# How make a single selection binding to a multiple selection in a gridview on winrt?

I come to you today because i don't know how to make a multiselection binding in a gridview. Today i know how to make a single and get the result but i don't know how to make this for multiple selections. Do you have any ideas ?

This is my code :

  public sealed partial class Base1 : ApplicationName.Common.LayoutAwarePage, INotifyPropertyChanged
{
    private ObservableCollection<Base> projects;
    public ObservableCollection<Base> Projects
    {
        get { return projects; }
        set
        {
            projects = value;
            NotifyPropertyChanged();
        }
    }


    private Base selectedProject;
    public Base SelectedProject
    {
        get {
            return selectedProject;
        }
        set {
            selectedProject = value;
            NotifyPropertyChanged();
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyname = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }

There's an attached behavior - GridViewExtensions.BindableSelection in WinRT XAML Toolkit that you could try. The sample for ListViewExtensions.BindableSelection here shows how you would use it. Just bind GridViewExtensions.BindableSelection to an ObservableCollection and they should stay in sync.

In the constructor of Base1 :

this.SelectedProjects = new ObservableCollection<Base>();

And add the property :

private ObservableCollection<Base> selectedProjects;
public ObservableCollection<Base> SelectedProjects
{
    get {
        return selectedProjects;
    }
    set {
        selectedProjects = value;
        NotifyPropertyChanged();
    }
}

And databind the property SelectedItems of your gridview with SelectedProjects.

You cant databind to SelectedItems property directly, since its readonly (i guess we are talking about wpf here). However, you can implement an IsSelected property in Base class and then bind it to IsSelected property of item container.

<Style TargetType="ListViewItem"> 
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>

You can then get selected items via something like

public IEnumerable<Base> SelectedProjects 
{
    get { return Projects.Where(x => x.IsSelected);}
}

i try a lot of things, but finally i found something really easy to use.

I just use "SelectedItems" of the GridView. After that i made what i want on thoose select items. If anyone wants more informations, i'll give.

Thanks a lot others who help me :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM