在 GridView 中显示上一个选择 [英] Show previous selection in GridView

查看:21
本文介绍了在 GridView 中显示上一个选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Windows 8 商店应用程序.如果来回导航,我想在 GridView 中显示以前选择的项目,所选项目应显示为已选择.我尝试过 本教程

I am developing windows 8 store app. I wants to show the previously selected items in GridView if navigate back and fro, the selected items should be shown selected.I have tried This tutorial

并完全按照建议进行.但它在我的情况下不起作用.我也试过用 index as

and did exactly as suggested. but its not working in my case. I have also tried with index as

 int index = myGridView.SelectedIndex

以便找到索引并直接提供

so that to find index and directly provide

myGridView.SelectedIndex = index ; 

但它又没用,因为我没有对

but its again not useful because I am not getting changes into the index in

SelectionChanged(object sender, SelectionChangedEventArgs e){};

有效的是

myGridView.SelectAll(); 

它选择所有元素.但我不想要这个.请帮我?提前致谢

it selects all the elements. but I don't want this. Please help me? Thanks in advance

请参考我的代码

<GridView x:Name="MyList" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" Padding="0" Height="600" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionMode="Multiple" SelectionChanged="names_SelectionChanged" ItemClick="mylist_ItemClick" SelectedItem="{Binding Path=selectedItem}">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Width="260" Height="80">                                    
                                <TextBlock Text="{Binding Path=Name}" Foreground="White" d:LayoutOverrides="Width" TextWrapping="Wrap"/>                                    
                            </StackPanel>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView>

这是我正在处理的课程

 public sealed partial class MyClass: MyApp.Common.LayoutAwarePage, INotifyPropertyChanged
{
    SQLite.SQLiteAsyncConnection db;

    public MyClass()
    {
        this.InitializeComponent();
        Constants.sourceColl = new ObservableCollection<MyModel>();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
       getData();
       foreach (MyModel item in Constants.sourceColl)
       MyList.SelectedItems.Add(item);
    }

    private async void getData()
    {
        List<MyModel> mod = new List<MyModel>();
        var query = await db.Table<MyModel>().Where(ch => ch.Id_Manga == StoryNumber).ToListAsync();
        foreach (var _name in query)
        {
            var myModel = new MyModel()
            {
                Name = _name.Name
            };

            mod.Add(myModel);
            Constants.sourceColl.Add(myModel);
        }
        MyList.ItemsSource = mod;
    }

    private void names_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
        GridView myGridView = sender as GridView;
        if (myGridView == null) return;
        Constants.sourceColl = (ObservableCollection<MyModel>)myGridView.SelectedItems;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private MyModel _selectedItem;

    public MyModel selectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
            if (_selectedItem != value)
            {
                _selectedItem = value;
                NotifyPropertyChanged("selectedItem");
            }
        }
    }
}

这是我的模型

class MyModel
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }
    public String Name { get; set; }
}

推荐答案

你好 rahul 我刚刚解决了你所面临的问题它不是完美的方法,但它可以在你的代码中工作.尝试遵循它.首先,我创建了一个单例类,用于存储您以前选择的项目(lstSubSelectedItems).就像这样

Hello rahul I have just solved the problem you are facing it is not the perfect way but it will work in your code. try to follow it. first I made a singleton class which store your previous selected items (lstSubSelectedItems)..like this

public class checkClass 
{
    static ObservableCollection<Subject> _lstSubSelectedItems = new ObservableCollection<Subject>();


    static checkClass chkclss;

    public static checkClass GetInstance()
    {
        if (chkclss == null)
        {
            chkclss =  new checkClass();
        }
        return chkclss;
    }



    public ObservableCollection<Subject> lstSubSelectedItems
    {
        get
        {
            return _lstSubSelectedItems;
        }
        set
        {
            _lstSubSelectedItems = value;
        }

    }

}

我已经在 pagenavigationfrom 方法上填充了 lstSubSelectedItems 像这样..这里 lstsub 是 selectedsubjects..

i have filled lstSubSelectedItems on pagenavigationfrom method like this.. here lstsub is selectedsubjects..

 protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        checkClass obj = checkClass.GetInstance();
        obj.lstSubSelectedItems = lstsub;
    }

这是我在构造函数中所做的解决方法...在这里,我使用 gridview.selecteditems 的 removeat 函数删除了非选定项,其他函数没有这样做(我不知道为什么).主题类就像你的模型类.并且 selecteditems 的设置也不起作用,这就是为什么我选择这种方式...希望能有所帮助.

Here is the workaround what I have done in my constructor... Here I removed the non selected items using removeat function of gridview.selecteditems other function are not doing this this for for (I don't know why). subject class is just like your model class . and also setting of selecteditems is not working that why I choose this way... Hope this help.

 public SelectSubject()
    {
        this.InitializeComponent(); // not required 
        objselectsubjectViewmodel = new SelectSubjectViewModel(); // not required 
        groupedItemsViewSource.Source = objselectsubjectViewmodel.Categories; // not required the way set the itemssource of grid.
        this.DataContext = this;
        checkClass obj = checkClass.GetInstance();

        if (obj.lstSubSelectedItems.Count > 0)
        {
         //   List<Subject> sjsfj = new List<Subject>();
            //    ICollection<Subject> fg = new ICollection<Subject>();
            itemGridView.SelectAll();
         //   int i = 0;
            List<int> lstIndex = new List<int>();



            foreach (Subject item1 in itemGridView.SelectedItems)
            {
                foreach (var item3 in obj.lstSubSelectedItems)
                {
                    if (item3.SubjectCategory == item1.SubjectCategory && item3.SubjectName == item1.SubjectName)
                    {
                        lstIndex.Add(itemGridView.SelectedItems.IndexOf(item1));
                    }
                }
            }

            int l = itemGridView.SelectedItems.Count;

            for (int b = l-1; b >= 0; b--)
            {
                if (!lstIndex.Contains(b))
                {
                    itemGridView.SelectedItems.RemoveAt(b);
                }
            }
        }


    }

告诉我它是否适合你...

tell me if it works for you...

这篇关于在 GridView 中显示上一个选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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