2012-04-26 15 views
1

galasoft mvvmテンプレートを使用して、かなり新しいwpfが新しくなりました。 同じことをする2つのrelayコマンドがありますが、それぞれ異なるプロパティを設定する必要があります。プロパティ名でプロパティを設定できますか?

私はこれを行うことはできますか?

public RelayCommand OpenDestinationCommand { get; private set; } 
public RelayCommand OpenSourceCommand { get; private set; } 

public MainViewModel(IDataService dataService) 
{ 
    OpenSourceCommand = new RelayCommand(() => GetPath(SourcePathPropertyName)); 
    OpenDestinationCommand = new RelayCommand(() => GetPath(DestinationPathPropertyName)); 
} 

private void GetPath(string PropertyName) { 
    //show a dialog, get the path they select 
    string newPath = GetPathFromDialog(); 
    //what should this look like? Is this possible? 
    var Property = GetPropertyByName(PropertyName); 
    Property.Set(newPath); 
} 

答えて

1

最初に見つけてください。 さておきhttp://geekswithblogs.net/shahed/archive/2006/11/19/97548.aspx

private PropertyInfo GetPropertyByName(string propName) 
{ 
    return this.GetType().GetProperty(propName); 
} 

private void GetPath(string PropertyName) { 
    //show a dialog, get the path they select 
    string newPath = GetPathFromDialog(); 
    //what should this look like? Is this possible? 
    var mProp = GetPropertyByName(PropertyName); 
    mPropp.SetValue(this, newPath, null); 
} 
+2

アンから適応:反射が比較的遅い* * - それは、それがタイトなループやホットパスにないよう大丈夫です。もしそうなら:より速いオプションがあります - あなたがこれを必要とするかどうか私に教えてください。 –

+0

もしあなたがより良い答えを持っていれば、それがあります!あまりにも多くの時間を費やすことはありませんが、perfは間違いなくここでは問題ではありません。 – scaryman

+0

wpfやmvvmのライトに何かが組み込まれていたら、私はほとんど興味がありました。私は完全にゾーン化して、質問を投稿したときの反射を忘れました。 – scaryman

関連する問題