初めて WPF を試していますが、他のオブジェクトの構成を使用して構築されたクラスにコントロールをバインドする方法に苦労しています。たとえば、2 つの別個のクラスで構成されるクラス Comp があるとします (わかりやすくするために、さまざまな要素が省略されていることに注意してください)。
class One {
int _first;
int _second;
}
class Two {
string _third;
string _fourth;
}
class Comp {
int _int1;
One _part1;
Two _part2;
}
これで、Comp で定義された「get」を使用して _int1 を簡単にバインドできることがわかりました。しかし、要素 _part1._first、_part1._second にバインドするにはどうすればよいですか。クラス Comp レベルで「ゲッター」を公開していますか? または、複合クラス内でそれらを公開し、それらを指すバインディング パスを使用できますか? そして、これはプロパティの設定でどのように機能しますか?
ということで、これがパターン?
....
<TextBlock Name="txtBlock" Text="{Binding Path=Third}" />
....
class One {
int _first;
int _second;
}
class Two {
string _third;
string _fourth;
}
class Comp {
int _int1;
One _part1;
Two _part2;
int Int1 { get { return _int1; } set { _int1 = value; } }
int First { get { return _part1._first; } set { _part1._first = value; } }
int Second { get { return _part1._second; } set { _part1._second = value; } }
string Third { get { return _part2._third; } set { _part2._third = value; } }
string Fourth { get { return _part2.fourth; } set { _part2._fourth = value; } }
}
...
Comp oComp = new Comp();
txtBlock.DataContext = oComp;
...
それともこのパターン?(パスに何を入れればよいかわからない場合)
....
<TextBlock Name="txtBlock" Text="{Binding Path=_part2.Third}" />
....
class One {
int _first;
int _second;
int First { get { return _first; } set { _first = value; } }
int Second { get { return _second; } set { _second = value; } }
}
class Two {
string _third;
string _fourth;
string Third { get { return _third; } set { _third = value; } }
string Fourth { get { return _fourth; } set { _fourth = value; } }
}
class Comp {
int _int1;
One _part1;
Two _part2;
int Int1 { get { return _int1; } }
}
...
Comp oComp = new Comp();
txtBlock.DataContext = oComp;
...
それとも、MV-VM を再発明しようとしているのですか (ゆっくりと理解し始めています)。
....
<TextBlock Name="txtBlock" Text="{Binding Path=Third}" />
....
class One {
int _first;
int _second;
}
class Two {
string _third;
string _fourth;
}
class Comp {
int _int1;
One _part1;
Two _part2;
}
class CompView {
Comp _comp;
CompView( Comp comp ) {
_comp = comp;
}
int Int1 { get { return _comp._int1; } set { _comp._int1 = value; } }
int First { get { return _comp._part1._first; } set { _comp._part1._first = value; } }
int Second { get { return _comp._part1._second; } set { _comp._part1._second = value; } }
string Third { get { return _comp._part2._third; } set { _comp._part2._third = value; } }
string Fourth { get { return _comp._part2.fourth; } set { _comp._part2._fourth = value; } }
}
...
Comp oComp = new Comp();
CompView oCompView = new CompView( oComp );
txtBlock.DataContext = oCompView;
...
では、どうすればよいのでしょうか。それが最初または 3 番目のパターンである場合、UI 要素にバインドできるように、素敵な (異種の) 階層データをすべて取り込んでフラットな構成にまとめたようです。これはどうあるべきか、それとももっと良い方法がありますか (2 番目のパターン??)
編集
私は本当に双方向バインディングが欲しいという質問を省きました。そのため、プロパティ アクセサーは本当に get と set を持つ必要があります。
編集
セッターとゲッターを表示するように擬似コードを更新しました
編集
Mark と Julien によって提供されたパターンに従い、セッターを実装したところ、結果に満足しました。何らかの理由で、プロパティの設定が最終的なエンティティまでずっと続くわけではないことを確信しました。