Automaticly Expand Objects

Mar 29, 2011 at 11:27 AM

I try to use Property Editor for display properties of a class. I have one problem about deeply object inside the class

For example

<AsyncQuery>

  <Book>
    <Authors>
      <string>aut1</string>
      <string>aut2</string>
      <string>aut3</string>
    </Authors>
    <Group>false</Group>
    <Show>false</Show>
    <SortOrder>NotSorted</SortOrder>
  </Book>

</AsyncQuery>

 

Authors is showed like System.Collections.Generic.List`1[System.String],

I think because is a sub sub property, is possible to expand multi level objects? I defined DataTemnplate for different objects at different levels but still stops at level 1

 

Thanks in advance

Coordinator
Apr 2, 2011 at 11:27 AM

The PropertyEditor control does not currently have support for expanding properties. But this can be done by implementing custom editors. I would use a data grid or an ItemsControl to edit the list of strings. First create a DataTemplate

 

<DataTemplate x:Key="AuthorListEditor">
    <Expander Header="Edit authors">
        <StackPanel>
            <ItemsControl ItemsSource="{Binding Value}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:Author}">
                        <TextBox Text="{Binding Name}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <Button Content="Add author" Command="{Binding Value.Add}"/>
        </StackPanel>
    </Expander>
</DataTemplate>

 

and then register the custom type editor:

<pt:PropertyEditor SelectedObject="{Binding SelectedBook}" >
    <pt:PropertyEditor.Editors>
        <pt:TypeEditor EditedType="{x:Type local:AuthorList}" EditorTemplate="{StaticResource AuthorListEditor}"/>
    </pt:PropertyEditor.Editors>
</pt:PropertyEditor>

I changed your model a little bit to simplify this example:

public enum SortOrder { Sorted, NotSorted };

public class Author
{
    public string Name { get; set; }
}

public class AuthorList : ObservableCollection<Author>
{
    public ICommand AddAuthor { get; set; }

    public AuthorList()
    {
        AddAuthor = new DelegateCommand(() => this.Add(new Author()), () => Count < 5);
    }
}

public class Book
{
    public AuthorList Authors { get; set; }
    public bool Group { get; set; }
    public bool Show { get; set; }
    public SortOrder SortOrder { get; set; }

    public Book()
    {
        Authors = new AuthorList
                        {
                            new Author {Name = "William Shakespeare"},
                            new Author {Name = "Agatha Christie"}
                        };
    }
}

Hope this helps, see the example I just checked into Examples/PropertyEditor/ExampleBrowser/Pages/CollectionExample.