1

wpf 相当新,使用 galasoft mvvm 模板。我有两个做同样事情的中继命令,但它们需要设置不同的属性。

我可以做这样的事情吗?

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

应该先google一下。改编自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 回答