1

galasoft mvvm テンプレートを使用して、wpf にかなり新しい。同じことを行う 2 つのリレー コマンドがありますが、異なるプロパティを設定する必要があります。

このようなことはできますか?

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

1 に答える 1

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);
}
于 2012-04-26T21:30:16.913 に答える