R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

WPF の ListBox のプロパティ

WPF の ListBox のデータバインディングの設定

で、ちょっと迷ったのでメモ。


[XAML]

  ItemsSource="{Binding Path=Rows}"
  DisplayMemberPath="Text"
  SelectedItem="{Binding Path=Row}"/>


[ViewModel]

public class HogeViewModel : INotifyPropertyChanged
{
  protected virtual void OnPropertyChanged(string propertyName) {
    if (this.PropertyChanged == null) return;
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }

  private Item _Row;
  public Item Row {
    get { return this._Row; }
    set {
      if (this._Row == value) return;
      this._Row = value;
      this.OnPropertyChanged("Row");
    }
  }

  private Item[] _Rows;
  public Item[] Rows {
    get { return this._Rows; }
    set {
      if (this._Rows == value) return;
      this._Rows = value;
      this.OnPropertyChanged("Rows");
    }
  }
}


[DataModel]

public class Item {
  private const int CODE_MAX_LENGTH = 8;
  public int Code { get; set; }
  public string Name { get; set; }
  public string Text { get { return this.ToString(); } }
  public override string ToString() {
      return this.Code.ToString().PadLeft(CODE_MAX_LENGTH, ' ') + " : " + this.Title;
  }
}


ItemsSource プロパティは、コレクションオブジェクトを

DisplayMemberPath プロパティは、コレクションオブジェクトの要素の内容を表現する文字列を

SelectedItem プロパティは、現在選択中の要素のオブジェクトを格納するためのオブジェクトを指定。

急いでまとめたので、コードの記述ミスがあったら指摘してください~

投稿日時 : 2009年7月1日 19:06

Feedback

# re: WPF の ListBox のプロパティ 2009/07/01 19:27 もり ひろゆき

んーと、DisplayMemberPathはBindingいらないのでは?

その名の通り、Pathだけ指定すればいいので単純に"Text"でいいんじゃなかったっけ?(^^;

# re: WPF の ListBox のプロパティ 2009/07/03 9:37 R・田中一郎

今試してみたら、Pathだけ指定していけるみたいなので修正しました^^;
でも、"{Binding Path=Text}" でも動くんですよね。

# UxGguHHMaUxFaBjMuG 2011/11/28 20:44 www.buytelescopes.com/Category/399-22-lr-pistols.a

Last a few years has been to Ibiza, so met a person there whose style of presentation is very similar to yours. But, unfortunately, that person is too far from the Internet!...

# IlXKBQeyFLjOm 2011/12/16 1:05 http://www.wildharvestedmaca.com/

I do`t regret that spent a few of minutes for reading. Write more often, surely'll come to read something new!...

# cMORBfjfiaOaiqD 2011/12/27 19:41 http://www.tat2x.com/

Yet, much is unclear. Could you describe in more details!...

# MJIFtAOYBEY 2011/12/29 21:01 http://www.healthinter.org/health/page/lumigan.php

Not bad post, but a lot of extra !!...

# syObPOhIJgFeeS 2012/01/07 9:33 http://www.luckyvitamin.com/m-207-optimum-nutritio

Not bad post, but a lot of extra !!...

# ロレックス 偽物 電池 2022/08/30 17:22 ixauzwtsaks@excite.co.jp

とてもきちんとした梱包で商品もきれいで
大変満足しました。丁寧で気持ちがよかったです。
ありがとうございました。
ロレックス 偽物 電池 https://www.b2kopi.com/product/detail.aspx-id=5454.htm

タイトル
名前
Url
コメント