これについて私を助けてくれるグルがどこかにいることを願っています.
Windows CE モバイル デバイス用のカスタム コントロールを作成しています。ユーザーが任意のソースをコントロールにバインドできるように、プロパティを作成しました。ソースを反復処理して、表示メンバー パスの項目値を取得する必要があります。
ソースを宣言してから、プロパティを作成します。コンストラクトでは、ソースが変更された場合にイベントを登録して、オブジェクトのペイントを再開できるようにします。この角度からバインディング ソースを操作するのはこれが初めてです。何かが足りないか、間違って使用していますか? ここにいくつかのコードスニペットがあります
private BindingSource _bindingCollection;
public object DataSource
{
set { _bindingCollection.DataSource = value; }
}
public string DisplayMemberPath { get; set; }
ctor()
{
_bindingCollection.DataSourceChanged += _bindingCollection_DataSourceChanged;
}
void _bindingCollection_DataSourceChanged(object sender, EventArgs e)
{
foreach (var item in _bindingCollection)
{
//I am stuck here on getting the value from the item that is
// specified by DisplayMemberPath to paint my objects on the control
//I can only paint on the control and cannot add any controls to it.
//So here a method is called with the value as a parameter and the
//Graphics object will draw the string
}
}
ありがとうございます。
よろしくリアーン
編集:
あなたが on paint メソッドを使用してペイントしていることは知っていますが、これは私がやりたいことの例です。すべてをゼロから作成し、コントロール全体をコードから描画し、必要なすべてのイベントをオーバーライドします。おそらく、DisplayMemberPath に _bindingCollection.DataMember を設定しますか? ソースを反復処理すると、アイテムが得られますか? 今すぐテストし、機能する場合は回答を投稿します。ペイントなどに使用するように指示するコメントや回答はもうやめてください。コレクションから表示メンバーの値を取得する問題に集中してください:)
編集:答えが見つかりました。申し訳ありませんが、これは実際には、多くのことで忙しいときに尋ねた本当にばかげた質問でした。これは実際にプロパティ値を取得するのは簡単でした。
for (int index = 0; index < _bindingCollection.Count; index++)
{
var propertyValue = _bindingCollection[index].GetType().GetProperty(DisplayMemberPath ).GetValue(_bindingCollection[index], null);
// Can do anthing now with the value here or just create a
//method of returning this value with this line of code on top.
}
誰かが質問を削除する必要がある場合は、質問を削除できます。このソリューションを急いでいる人が他にいて、入手方法がわからない場合は、ここに残します。これらは単なるコード スニペットであり、使用可能なメソッドではありません。