This project has moved and is read-only. For the latest updates, please go here.

Use a CollectionControl as an EditingTemplate.

Jun 9, 2015 at 3:04 PM
I'm using the PropertyGrid with a good deal of success. However, I'd like to change the behavior of the CollectionControl. Instead of having a new dialog appear for collections, I'd like to embed the CollectionControl directly into my PropertyGrid.

Suppose I have:
public class AddressBook
{
public string Owner { get; set; }
public bool IsUpToDate { get; set; }
public List<People> People { get; set; }
}

public class People
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
When I select the property People from AddressBook, rather than have a separate dialog show up, I'd like the CollectionControl template to appear inline with Owner and IsUpToDate.

I tried to do this with an Editing Template:
<xctk:PropertyGrid.EditorDefinitions>
  <xctk:EditorTemplateDefinition TargetProperties="People">
    <xctk:EditorTemplateDefinition.EditingTemplate>
      <DataTemplate>
        <xctk:CollectionControl ItemsSource="{Binding Value}" />
      </DataTemplate>
    </xctk:EditorTemplateDefinition.EditingTemplate>
  </xctk:EditorTemplateDefinition>
</xctk:PropertyGrid.EditorDefinitions>
This LOOKED the way I wanted it to, but I noticed that any changes I made to People (adding people, removing people, changing the properties of a People object) were not saved back to my source.

Any suggestions? Is this even possible/recommended?

Thanks!
Developer
Jun 9, 2015 at 9:38 PM
Hi,

The changes done in the CollectionControl are saved in the source when the OK button from the CollectionControlDialog is pressed.
You can save the changes by calling CollectionControl.PersistChanges() when items are added, deleted...
<xctk:PropertyGrid.EditorDefinitions>
        <xctk:EditorTemplateDefinition TargetProperties="People">
           <xctk:EditorTemplateDefinition.EditingTemplate>
              <DataTemplate>
                 <xctk:CollectionControl ItemsSource="{Binding Value}"
                                         ItemAdded="CollectionControl_Action"
                                         ItemDeleted="CollectionControl_Action"
                                         ItemMovedDown="CollectionControl_Action"
                                         ItemMovedUp="CollectionControl_Action"/>
              </DataTemplate>
           </xctk:EditorTemplateDefinition.EditingTemplate>
        </xctk:EditorTemplateDefinition>
</xctk:PropertyGrid.EditorDefinitions>

private void CollectionControl_Action( object sender, Xceed.Wpf.Toolkit.ItemEventArgs e )
{
  var collectionControl = sender as CollectionControl;
  if( collectionControl != null )
  {
    collectionControl.PersistChanges();
  }
}
Marked as answer by whalenJP on 6/10/2015 at 9:46 AM
Jun 10, 2015 at 5:46 PM
Worked perfectly! Thanks!