简体   繁体   English

C#如何在Winrt的gridview中将单个选择绑定到多个选择?

[英]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. 我今天来找您,是因为我不知道如何在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. 有一个附加的行为-您可以尝试GridViewExtensions.BindableSelection WinRT XAML Toolkit中的GridViewExtensions.BindableSelection The sample for ListViewExtensions.BindableSelection here shows how you would use it. 这里ListViewExtensions.BindableSelection示例显示了如何使用它。 Just bind GridViewExtensions.BindableSelection to an ObservableCollection and they should stay in sync. 只需将GridViewExtensions.BindableSelection绑定到ObservableCollection ,它们应该保持同步。

In the constructor of Base1 : 在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. 然后将gridview的SelectedItems 属性与SelectedProjects绑定。

You cant databind to SelectedItems property directly, since its readonly (i guess we are talking about wpf here). 您不能直接将数据绑定到SelectedItems属性,因为它是只读的(我想我们在这里谈论的是wpf)。 However, you can implement an IsSelected property in Base class and then bind it to IsSelected property of item container. 但是,您可以在基类中实现IsSelected属性,然后将其绑定到项目容器的IsSelected属性。

<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. 我只是使用GridView的“ SelectedItems”。 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 :) 非常感谢其他帮助我的人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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