是否有任何数据,结合框架(BCL或否)允许之间的结合 任何两个CLR性 实现 INotifyPropertyChangedINotifyCollectionChanged?这似乎是应该尽可能做这样的事情:

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

哪里 someSourceObjectsomeTargetObject 只是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);
                }
            };
    }
}

当然,这个代码缺乏一些细节。事增加包括

  • 检查 sourcetarget 分配
  • 检查的性质确定的 sourcePropertyNametargetPropertyName 存在
  • 检查类型之间的兼容性两种特性

此外,反映的是相对较缓慢(虽然基准它之前放弃它,也不是 慢),因此可能需要使用编制的表达,而不是。

最后,鉴于指定性质的串是容易出现错误,可使用皇宫表现形式和扩展的方法代替。那不是写作

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

你可以写

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

AutoMapper 可以在两个实例之间复制值,但您必须编写自己的代码才能实现此目的自动发生。

也许 Bindable LINQ 连续的linq 可以在这里提供帮助。如果您尝试添加实际上是“派生属性”的模型属性,为了让UI更容易绑定到您的实际更新数据,这两个框架应该有所帮助。

我写了一个小的 Bind 项目,完全支持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);

如果您将属性定义为 DependencyProperty 你可以做到的。 WF和WPF都有它的实现(第一个链接用于WPF。对于WF,它是这个)所以你需要决定使用哪个 - 但两者都应该满足你的需求。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top