Rahul Saksule
Rahul Saksule

Reputation: 417

Show previous selection in 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

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){};

What works is

myGridView.SelectAll(); 

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

Please refer my code

<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>

This is The class I am dealing with

 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");
            }
        }
    }
}

Here is my model

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

Upvotes: 0

Views: 656

Answers (2)

loop
loop

Reputation: 9242

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;
        }

    }

}

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;
    }

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...

Upvotes: 1

loop
loop

Reputation: 9242

You can set selectedItems property of gridView for doing this first make observableCollection and the continuously update this collection on selectionchange Event of your gridView . and when you comeback to this page set the GridViewName.SelectedItems = aboveCollection;

  private ObservableCollection<Subject> lstsub = new ObservableCollection<Subject>() ;



    private void itemGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
       checkTemp = 1;
        GridView tempObjGridView = new GridView();
        tempObjGridView = sender as GridView;  
        lstsub = tempObjGridView.SelectedItems;
    }
 protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
       yourGridName.SelectedItems = lstsub ;
    }

Upvotes: 0

Related Questions