Telerik blogs

In Building Conference Buddy for WPF (described here with an overview here), Carey and I knew that we would have a substantial form to fill in for the details about each event.

You will remember that Conference Buddy is a system for tracking people we meet at conferences, and which Telerik products they are using and which they are interested in.

We already had a substantial Event class from the Win8 version with about 18 properties.  Just before creating the form by hand, we read Michael Crump’s excellent article Data Forms in your XAML LOB apps got you down? Let RadDataForm createe your UI!  We dropped a RadDataForm into our application and hey! presto! we had a form to gather and edit the data on each event. 

InstantForm

The form took care of all the CRUD operations on the underlying data (which was in an observable collection) but we wanted to be able to write the data out to a JSON file each time it changed.  What we needed was to hook up a command to some notification of an edit or a delete completion.

Enter Telerik.Windows.Controls.Data.DataForm.DataFormCommandProvider – a public class that exposes the “execute” and “can-execute” logic of all commands utilized by RadDataForm.  The Execute logic methods include CommmitEdit and Delete – the two we need.

Step one was to create a class derived from DataFormCommandProvider which implemented two constructors: a default constructor and one which took a RadDataForm,

public class CustomRadDataFormCommandProvider :
                          DataFormCommandProvider
{
   public CustomRadDataFormCommandProvider()
      : base( null )
   {
   }
 
   public CustomRadDataFormCommandProvider( RadDataForm dataForm )
      : base( dataForm )
   {
      this.DataForm = dataForm;
   }

 

The next step was to override both the CommitEdit and the Delete methods. We want to write the JSON out to a file in both cases, so we factored that logic out into a helper method,

protected override void CommitEdit()
{
   base.CommitEdit();
   PersistEvents();
}
 
protected override void Delete()
{
   base.Delete();
   PersistEvents();
}
 
private void PersistEvents()
{
   EventRepository repo = new EventRepository();
   var events =
      ( this.DataForm.ItemsSource as IEnumerable<Event> ).
                                ToList<Event>();
   repo.WriteToJSON( events );
}

 

With that code in place, we were ready to wire up to our RadDataForm.  First step was to create a resource,

<UserControl.Resources>
 <local:CustomRadDataFormCommandProvider x:Key="CommandProvider" />
</UserControl.Resources>

 

With that in place, we could assign it to the CommandProvider property of our DataForm,

<telerik:RadDataForm Header="Edit Events"
                     ItemsSource="{Binding TheEvents, Mode=TwoWay}"
                     CommandProvider=
                             "{StaticResource CommandProvider}" />

 

Each time we create, update or delete a record the CommandProvider allows us to call PersistEvents and write our JSON to disk (and up to our server, for that matter). 

I love a solution that is simple and effective.

Telerik DevCraft Q2 2013 Webinar Week


jesseLiberty
About the Author

Jesse Liberty

 has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog or follow him on twitter

Comments

Comments are disabled in preview mode.