1

リストがGridviewにバインドされるモデルオブジェクトがあります。これは私のモデルクラスです

public class ItemModel: BindableBase
{


    public override bool Equals(object obj)
    {
        return this.Id == ((ItemModel)obj).Id;
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
    public static bool operator ==(ItemModel op1, ItemModel op2)
    {
        return  op1.Id == op2.Id;
    }

    public static bool operator !=(ItemModel op1, ItemModel op2)
    {
        return op1.Id != op2.Id;
    }



    private Guid _id;
    public Guid Id
    {
        set { SetProperty(ref this._id, value); }
        get { return _id; }
    }

    private string _clue;
    public string Clue
    {
        set { this.SetProperty(ref this._clue, value); }
        get { return _clue; }
    }

    private string _boxId;
    public string BoxId
    {
        set { this.SetProperty(ref this._boxId, value); }
        get { return _boxId; }
    }

    public override string ToString()
    {
        return this.Clue;
    }

マイページにデータをロードする方法は 2 つあります。1 番目は整数で、2 番目は ItemModel オブジェクトです。2 番目の方法では、ページに渡された対応するアイテムをグリッドで選択する必要があります。私のコードは次のとおりです。

protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
        // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"]

        if (navigationParameter is int)
        {
            BindByBoxId(navigationParameter);
        }
        else if (navigationParameter is ItemModel)
        {
            BindByWordObject(navigationParameter as ItemModel);
        }

    }

    private async void BindByWordObject(ItemRepository passedItem)
    {
        ItemRepository repo = new ItemRepository();
        var boxId = (int)passedItem.BoxId;
        var item = await repo.GetWordsByBox(boxId);
        this.DefaultViewModel["Items"] = item;
        GroupNameTextBlock.Text = passedItem.Box.BoxName;


        itemGridView.SelectedIndex = GetItemIndex(passedItem);
        itemGridView.SelectionChanged += SelectChanged;

    }

    private int GetItemIndex(ItemModel item)
    {
        for(int i =0 ; i<itemGridView.Items.Count; i++)
        {
            if (((ItemModel)itemGridView.Items[i]) == item)
                return i;
        }
        return -1;
    }



    private async void BindByBoxId(object navigationParameter)
    {
        ItemRepository repo = new ItemRepository();
        var boxId = (int)navigationParameter;
        var item = await repo.GetItemsByBox(boxId);
        this.DefaultViewModel["Items"] = item;
        GroupNameTextBlock.Text = item.First().Box.BoxName;
        itemGridView.SelectedValue = -1;
        itemListView.SelectedValue = -1;

        itemGridView.SelectionChanged += SelectChanged;
    }

私は試しSelectedValueてみSelectedItemました(Windowsストアアプリケーションでは、 seter および geter として使用できます)が、これらのプロパティはうまく機能しません。グリッドビューで対応するアイテムを選択するための私の解決策は、グリッドビューのアイテムをループして、アイテムのインデックス (GetItemIndexメソッド) を見つけ、SelectedIndexプロパティを使用することです。私にとって最適化された別の方法があるか知りたいですか?多数のアイテムをループするのはひどいので!!! 私にアドバイスしてください。

4

1 に答える 1