首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用DynamicObject模拟DependencyProperties

用DynamicObject模拟DependencyProperties
EN

Stack Overflow用户
提问于 2012-03-18 08:38:23
回答 1查看 461关注 0票数 2

我希望创建类似VS或Blend特性之一的东西,当您选择多个对象时,属性网格将显示所有对象共享的任何属性的值,而对于对象之间不同的属性则不显示任何值。

我使用一个动态对象为CLR属性实现了这种行为:

  • _knownProperties只是一个以前是requested
  • _collection的属性列表,是一个IEnumerable实例

代码语言:javascript
复制
public override bool TryGetMember( GetMemberBinder binder, out object result ) {
    Debug.WriteLine( "Getting " + binder.Name + "..." );

    if (!_knownProperties.Contains( binder.Name ))
        _knownProperties.Add( binder.Name );

    IEnumerator it = _collection.GetEnumerator();

    if (!it.MoveNext()) {
        result = null;
        Debug.WriteLine( "No elements in collection" );
        return true;
    }

    Type t = it.Current.GetType();
    PropertyInfo pinf = t.GetProperty( binder.Name );
    if (pinf == null) {
        result = null;
        Debug.WriteLine( "Property doesn't exist." );
        return true;
    }
    result = pinf.GetValue( it.Current, null );

    if (result == null) {
        Debug.WriteLine( "Null result" );
        return true;
    }
    while (it.MoveNext())
        if (!result.Equals( it.Current.GetType().GetProperty( binder.Name ).GetValue( it.Current, null ) )) {
            result = null;
            Debug.WriteLine( "Null result" );
            return true;
        }

    Debug.WriteLine( "Result: " + result.ToString() );
    return true;
}

我正在通过WPF绑定访问这些属性。有人能想出一种在DependencyProperties中实现这一点的方法吗?如果我试图绑定到对象上的附加属性,则在属性系统中得到一个ArgumentNullException (其中,根据我的源,空的对象不可能是空的)。

  • {Binding Selection.SomeClrProperty,...}工作正常(Selection是动态对象之一,SomeClrProperty是collection.
  • {Binding Selection.(SomeClass.SomeAttachedProperty),...}的每个元素上的一个属性,在属性系统

中触发一个错误)。

例外情况:

代码语言:javascript
复制
System.ArgumentNullException was unhandled
Message=Key cannot be null.
Parameter name: key
Source=System
ParamName=key
StackTrace:
   at System.Collections.Specialized.HybridDictionary.get_Item(Object key)
   at System.ComponentModel.PropertyChangedEventManager.PrivateAddListener(INotifyPropertyChanged source, IWeakEventListener listener, String propertyName)
   at System.ComponentModel.PropertyChangedEventManager.AddListener(INotifyPropertyChanged source, IWeakEventListener listener, String propertyName)
   at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32 k, Object newO, Object parent)
   at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.AttachDataItem()
   at System.Windows.Data.BindingExpression.Activate(Object item)
   ...
EN

回答 1

Stack Overflow用户

发布于 2012-09-20 23:37:16

使用OneTime绑定可以防止WPF试图在附加属性上放置侦听器:

代码语言:javascript
复制
{Binding Selection.(SomeClass.SomeAttachedProperty), Mode=OneTime, ...}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9757037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档