2016-04-22 12 views
0

WPF Toolkit CEのプロパティグリッドを使用します。 オブジェクトをコレクションに追加すると、開いたダイアログにコンボボックスが表示され、追加する新しいタイプを選択します。ドキュメントでは、NewItemTypesAttributeを読みました。 この属性は、選択したオブジェクトのコレクションプロパティ(つまり、IList)を飾って、CollectionControlでインスタンス化を許可するタイプを制御できます。WPF Toolkit Community Editionプロパティグリッド:コレクションエディタの新しいアイテムタイプ

しかし、私はそれを働かせることはできません。この場合

namespace TestPropertyGrid 
{ 
    /// <summary> 
    /// Interaktionslogik für MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
    public MainWindow() 
    { 
     InitializeComponent(); 

     MyObject myObj = new MyObject(); 
     this.PGrid.SelectedObject = new MyObject(); 
    } 
    } 

    public class MyObject 
    { 
    [NewItemTypes(typeof(MyBaseObj), typeof(MyObj1))] 
    [Editor(typeof(CollectionEditor), typeof(CollectionEditor))] 
    public ArrayList ListOfObjects { get; set; } = new ArrayList(); 
    } 

    public class MyBaseObj 
    { 
    } 

    public class MyObj1 
    { 
    } 
} 

を「選択型」のリストが空である:ここでは は、私が試した最後の変種です。

ArrayListの代わりにList<object>を使用しようとしましたが、リストにはオブジェクト型のみが含まれています。

はで初期状態私はList<MyBaseObj>を望んでいた(抽象クラ​​スになります)との両方がMyBaseObjから継承するタイプMyObj1 RESP MyObj2のオブジェクトを追加します。ドキュメントサンプルと同じ:

public partial class MainWindow : Window 
    { 
    public MainWindow() 
    { 
     InitializeComponent(); 
     Community myObj = new Community(); 
     myObj.Members = new List<Person>(); 
     this.PGrid.SelectedObject = myObj; 
    } 
    } 

    public class Community 
    { 
    [NewItemTypes(typeof(Man), typeof(Woman))] 
    [Editor(typeof(CollectionEditor), typeof(CollectionEditor))] 
    public IList<Person> Members { get; set; } 
    } 

    public class Person { } 
    public class Man : Person { } 
    public class Woman : Person { } 

私は私の問題をはっきりと説明することができ、誰かが助けてくれることを願っています。ありがとうございました。

答えて

0

奇妙な...これは100%実装されていないようです。 今私は独自のCollectionEditorを作成し、この属性から型を自分で設定しました。

class MyCollectionEditor : TypeEditor<CollectionControlButton> 
{ 
    protected override void SetValueDependencyProperty() 
    { 
    ValueProperty = CollectionControlButton.ItemsSourceProperty; 
    } 

    protected override void ResolveValueBinding(PropertyItem propertyItem) 
    { 
    var type = propertyItem.PropertyType; 
    Editor.ItemsSourceType = type; 
    // added 
    AttributeCollection attrs = propertyItem.PropertyDescriptor.Attributes; 
    Boolean attrFound = false; 
    foreach(Attribute attr in attrs) 
    { 
     if (attr is NewItemTypesAttribute) 
     { 
     Editor.NewItemTypes = ((NewItemTypesAttribute)attr).Types; 
     attrFound = true; 
     break; 
     } 
    } 
    // end added 
    if (!attrFound) 
    { 
     if (type.BaseType == typeof(System.Array)) 
     { 
     Editor.NewItemTypes = new List<Type>() { type.GetElementType() }; 
     } 
     else if (type.GetGenericArguments().Count() > 0) 
     { 
     Editor.NewItemTypes = new List<Type>() { type.GetGenericArguments()[0] }; 
     } 
    } 
    base.ResolveValueBinding(propertyItem); 
    } 
} 
関連する問題