문제

간의 바인딩을 허용하는 데이터 바인딩 프레임 워크 (BCL 또는 기타)가 있습니까? 두 개의 CLR 속성 그 구현 INotifyPropertyChanged 그리고 INotifyCollectionChanged? 다음과 같은 일을 할 수있는 것 같습니다.

var binding = new Binding();
binding.Source = someSourceObject;
binding.SourcePath = "Customer.Name";
binding.Target = someTargetObject;
binding.TargetPath = "Client.Name";
BindingManager.Bind(binding);

어디에 someSourceObject 그리고 someTargetObject 구현하는 Pocos 일뿐입니다 INotifyPropertyChanged. 그러나 나는 이것에 대한 BCL 지원을 알지 못하며,이를 허용하는 기존 프레임 워크가 있는지 확실하지 않습니다.

업데이트: 기존 라이브러리가 없다는 것을 감안할 때, 나는 내 자신의 글을 쓰기 위해 스스로 가져갔습니다. 사용할 수 있습니다 여기.

감사

도움이 되었습니까?

해결책

나는 썼다 지붕틀 공허를 채우기 위해.

다른 팁

나는 아무것도 모른다 도서관 그렇습니다. 그러나 당신은 자신의 것을 상당히 쉽게 쓸 수 있습니다.

다음은 몇 분 안에 두 가지 간단한 속성 사이에 두 가지 방법 데이터 바인딩을 설정하는 기초가 있습니다.

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 표현식 및 확장 방법을 대신 사용할 수 있습니다. 그런 다음 글을 쓰는 대신

Binder.Bind( source, "Name", target, "Name")

당신은 쓸 수 있습니다

source.Bind( Name => target.Name);

automapper 두 인스턴스 사이에 값을 복사 할 수 있지만이를 자동으로 수행하려면 고유 한 코드를 작성해야합니다.

아마도 바인딩 가능한 linq 또는 연속 LINQ 여기서 도울 수 있습니다. 실제 데이터의 "파생 된 속성"인 모델 속성을 추가하려면 UI가 쉽게 바인딩 할 수 있도록이 두 프레임 워크가 도움이 될 것입니다.

나는 작은 것을 썼다 묶다 Neasted 특성 비동기 결합 작용 사이의 결합에 대한 완전한 지원을 가진 프로젝트. sintax는 더 간단 할 수 없습니다.

//Two way binding between neasted properties:
Bind.TwoWay(()=> client.Area.Data.Name == this.AreaName);

//On change action execute:
Bind
    .OnChange(()=> client.Personal.Name)
    .Do(x => clientName = x);

속성을 정의 한 경우 의존성 분야당신은 그것을 할 수 있습니다. WF와 WPF는 모두 IT를 구현했습니다 (첫 번째 링크는 WPF입니다. 이것 하나) 따라서 사용할 것인지 결정해야하지만 둘 다 필요에 충분해야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top