- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- wp7/tips/listbox_timing へ行く。
- 1 (2011-12-14 (水) 14:20:20)
ListBox のアイテム描画の完了を確認する方法
サンプルプロジェクト ListBox_Timing.zip
ListBox のアイテム表示の方法は、ListBox.ItemSource に表示するコレクションをバインドするのが一般的な方法で、みなさんも良くお使いなのではないでしょうか。
WinForm の頃のように ListBox.Items.Add メソッドでアイテムを追加する事は少なくなりましたね。
しかしバインドしたときに描画が完了した事が分からないという問題があります。
public ObservableCollection<string> Items { get; set; }
// コンストラクター
public MainPage()
{
InitializeComponent();
this.DataContext = this;
this.listbox.LayoutUpdated += new EventHandler(listbox_LayoutUpdated);
}
int Count
{
get
{
int count = this.listbox.ItemsSource == null ? 0 : ((ICollection)this.listbox.ItemsSource).Count;
return count;
}
}
void listbox_LayoutUpdated(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("listbox_LayoutUpdated " + this.Count);
// 描画完了後のスクロールなのでちゃんとスクロールする。
if (this.Items != null && this.Count == this.Items.Count)
{
this.listbox.ScrollIntoView(this.Items[this.Items.Count - 1]);
}
}


