2012-04-26 4 views
1

galasoft mvvm 템플릿을 사용하여 상당히 새로운 wpf를 사용합니다. 같은 일을하는 두 개의 relaycommand가 있지만 서로 다른 속성을 설정해야합니다.속성 이름으로 속성을 설정할 수 있습니까?

이렇게 할 수 있습니까?

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

당신이 더 좋은 대답을 가지고 있다면 그것을 가지고있어! 너무 많은 시간을 소비하지 마십시오. 퍼포먼스는 확실히 여기서 문제가되지 않습니다. – scaryman

+0

wpf 및/또는 mvvm 라이트에 내장 된 것이 있으면 대부분 궁금합니다. 내가 완전히 질문을 올렸을 때 나는 반성했다. – scaryman

관련 문제