2

VS または Blend の機能の 1 つに似たものを作成しようとしています。複数のオブジェクトを選択すると、プロパティ グリッドにはすべてのオブジェクトで共有されるプロパティの値が表示され、オブジェクト間で異なるプロパティには何も表示されません。

動的オブジェクトを使用して、CLR プロパティにこの動作を実装することができました。

  • _knownProperties以前にリクエストされたプロパティの単なるリストです
  • _collectionIEnumerable インスタンスです
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、プロパティ システムで取得されます (ここで、null であるオブジェクトは、私が持っているソースによると null になることはできません) 。

  • {Binding Selection.SomeClrProperty,...}正常に動作します (Selectionは動的オブジェクトの 1 つでSomeClrProperty、コレクションのすべての要素のプロパティです。
  • {Binding Selection.(SomeClass.SomeAttachedProperty),...}プロパティ システムでエラーを発生させます

例外:

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)
   ...
4

1 に答える 1

0

OneTime バインディングを使用して、WPF が添付プロパティにリスナーを配置しようとするのを防ぎます。

{Binding Selection.(SomeClass.SomeAttachedProperty), Mode=OneTime, ...}
于 2012-09-20T23:37:16.090 に答える