저는 라이브러리에 대해 알지 못합니다. 그러나 당신은 매우 쉽게 직접 작성할 수 있습니다.
저는 여기에 간단한 두 속성 사이의 바인딩 두 가지 방법으로 데이터를 설정하는 몇 분 노크 근거는 다음과 같습니다 물론
public static class Binder
{
public static void Bind(
INotifyPropertyChanged source,
string sourcePropertyName,
INotifyPropertyChanged target,
string targetPropertyName)
{
var sourceProperty
= source.GetType().GetProperty(sourcePropertyName);
var targetProperty
= target.GetType().GetProperty(targetPropertyName);
source.PropertyChanged +=
(s, a) =>
{
var sourceValue = sourceProperty.GetValue(source, null);
var targetValue = targetProperty.GetValue(target, null);
if (!Object.Equals(sourceValue, targetValue))
{
targetProperty.SetValue(target, sourceValue, null);
}
};
target.PropertyChanged +=
(s, a) =>
{
var sourceValue = sourceProperty.GetValue(source, null);
var targetValue = targetProperty.GetValue(target, null);
if (!Object.Equals(sourceValue, targetValue))
{
sourceProperty.SetValue(source, targetValue, null);
}
};
}
}
이 코드는 몇 가지 미묘한 없다. 추가 할 수있는 상황이
- 이
source
및 target
이 sourcePropertyName
및 targetPropertyName
에 의해 식별되는 특성이
- 이 두 가지 속성 또한
사이의 유형 호환성을 확인 존재 함을 확인
을 할당되었는지 확인을 포함, 반사 상대적으로 느립니다 (벤치마킹하기 전에 그것을 버리기 전에 이 아니기 때문에이 느립니다). 따라서 컴파일 된 표현 대신. 마지막으로 문자열로 속성을 지정하면 오류가 발생하기 쉽기 때문에 대신 Linq 식과 확장 메서드를 사용할 수 있습니다. 당신은 당신이 그것을 할 수 DependencyProperty 년대로 속성을 정의하면 다음 대신
Binder.Bind(source, "Name", target, "Name")
을 쓰는 당신은
source.Bind(Name => target.Name);
나는 나의 자신의 쓰기를 고려하고 있었기 때문에 실제로 요구하고 있었다. 바퀴와 모든 것을 재발견하고 싶지 않았어 ... 고마워. –
업데이트 : 질문에 내 라이브러리에 연결했습니다. –
저는 현 프로젝트에 대해 간단한 barebone 데이터 바인딩 클래스가 필요했습니다. 그리고 위의 방법은 내가 필요로하는 것과 거의 일치합니다. Reflection에 사용 된 속성 이름을 POCO에서 가져 오거나 설정하고 변환 및 서식을 적용하는 두 개의 작업 대리자로 대체했습니다. 나는 다음 프로젝트에서 트러스에게 좋은 시도를 할 것입니다. 왜냐하면 그것은 정말로 나에게 들리는 것 같기 때문입니다. – Larry