3

バインド可能なコレクション (特殊なスタック) を実装しようとしています。これは、Windows 8 アプリの 1 つのページに表示する必要があり、更新が行われるたびに表示されます。このために、INotifyCollectionChanged と IEnumerable<> を実装しました。

public class Stack : INotifyCollectionChanged, IEnumerable<Number>
{

...

public void Push(Number push)
{
    lock (this)
    {
        this.impl.Add(push);
    }

    if (this.CollectionChanged != null)
        this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, push));
}

...and the equivalents for other methods...

#region INotifyCollectionChanged implementation

public event NotifyCollectionChangedEventHandler CollectionChanged;

#endregion

public IEnumerator<Number> GetEnumerator()
{
    List<Number> copy;

    lock (this)
    {
        copy = new List<Number>(impl);
    }
    copy.Reverse();

    foreach (Number num in copy)
    {
        yield return num;
    }
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
    return this.GetEnumerator();
}

このコレクション クラスは、ページが所有する基になるクラス インスタンスのプロパティを定義するために使用されます。これは、その DataContext (ページの Calculator プロパティ) として設定され、GridView にバインドされます。

<GridView x:Name="StackGrid" ItemsSource="{Binding Stack, Mode=OneWay}" ItemContainerStyle="{StaticResource StackTileStyle}" SelectionMode="None">

... ItemTemplate omitted for length ...

バインディングは、ページが移動されたときに最初に機能します。スタック内の既存のアイテムは問題なく表示されますが、スタックに追加された/スタックから削除されたアイテムは、ページが移動されて戻るまで GridView に反映されません。デバッグにより、スタック内の CollectionChanged イベントが常に null であるため、更新時に呼び出されないことがわかります。

私は何が欠けていますか?

4

1 に答える 1

0

ちょうど今、バインド可能にしたいカスタムコレクションで同じ問題に直面しています。クラスから派生したフォームのみをCollection<>バインドできることがわかりました。

なんで?今のところわかりません。したがって、本当に機能させたい場合は、フォームを派生Collection<>させますが、これはデザインを台無しにします。

于 2013-10-26T07:43:04.577 に答える