• 追加された行はこの色です。
  • 削除された行はこの色です。
#setlinebreak(on);
[[WP TIPS に戻る>wp7/tips]]

*ListBox のアイテム描画の完了を確認する方法 [#g60c7566]
サンプルプロジェクト [[ListBox_Timing.zip>https://skydrive.live.com/redir.aspx?cid=793b87c06d2f0cd5&resid=793B87C06D2F0CD5!1901&parid=793B87C06D2F0CD5!223]]

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