0

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

并完全按照建议进行。但它不适用于我的情况。我也尝试过使用索引作为

 int index = myGridView.SelectedIndex

以便找到索引并直接提供

myGridView.SelectedIndex = index ; 

但它又没用,因为我没有对索引进行更改

SelectionChanged(object sender, SelectionChangedEventArgs e){};

有效的是

myGridView.SelectAll(); 

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

请参考我的代码

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

2 回答 2

1

您好 rahul 我刚刚解决了您面临的问题,这不是完美的方法,但它可以在您的代码中使用。尝试跟随它。首先我做了一个单例类来存储你以前选择的项目(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 ..

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

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

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


    }

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

于 2013-07-09T06:58:02.257 回答
0

您可以设置 gridView 的 selectedItems 属性,以便首先制作 observableCollection 并在您的 gridView 的 selectionchange 事件上不断更新此集合。当你回到这个页面时,设置 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 ;
    }
于 2013-07-08T11:35:28.577 回答