Custom Views

If you are not satisfied with the built-in views (editors) for certain supported options data types, you can easily change that by providing an own view factory. The interface is really simple:

/// <summary>
/// Describes a factory that is able to create views for options.
/// </summary>
public interface IOptionsViewFactory
{
    /// <summary>
    /// Creates a view for a given option instance.
    /// </summary>
    /// <param name="option">The option to create the view for.</param>
    /// <returns>A framework element that is able to edit the given option.</returns>
    FrameworkElement CreateViewFor(Option option);
}

As you can see, all the factory has to do is create and provide a framework element for a given option (which is one of the derived option types, actually). To get an idea of a real world implementation, I strongly recommend that you take a look at the "Extras" project, because it makes use of the exact same mechanism to extend the base functionality:

public class ExtrasOptionsViewFactory : IOptionsViewFactory
{
    private readonly DefaultOptionsViewFactory _defaultFactory = new DefaultOptionsViewFactory();

    /// <summary>
    /// Creates a view for a given option instance.
    /// </summary>
    /// <param name="option">The option to create the view for.</param>
    /// <returns>A framework element that is able to edit the given option.</returns>
    public FrameworkElement CreateViewFor(Option option)
    {
        FrameworkElement result = null;

        if (option is TimeSpanOption)
        {
            result = new TimeSpanEditor();
        }
        else if (option is ColorOption)
        {
            result = new ColorEditor();
        }

        // are we supporting this kind of option?
        if (result != null)
        {
            result.DataContext = option;
            return result;
        }

        // for each option type we don't support, let the default factory handle it
        return _defaultFactory.CreateViewFor(option);
    }
}

As you can see, the "Extras" view factory checks for certain data types and provides its own implementations for them, and routes all other requests to an instance of the default factory of the library. Another alternative would be to derive from the default factory and override the interface method. For example, if you're not satisfied with the fact that the default editor for boolean values uses a ToggleSwitch, you can create a view factory similar to the one above, and then intercept all requests for BooleanOption options. Provide your own view (usually a user control) that e.g. in this case uses a CheckBox or similar instead.

The last thing to do is to configure the OptionsService to actually use the factory (it uses a default factory if no other is configured). The way this works simply is:

OptionsService.Current.OptionsViewFactory = new MyCustomOptionsViewFactory();

It doesn't matter where you configure this, as long as you do it before you call the Show method of the options service (that consequently uses that factory to create the views). Please note that you need to repeat the factory configuration above when the user returns to the application after a tombstoning situation; for more details, see the documentation section on tombstoning.

Last edited Nov 20, 2011 at 4:17 PM by Mister_Goodcat, version 1

Comments

No comments yet.