You are currently browsing the category archive for the ‘WPF’ category.

I would like to show an example of application that is about how to use Fluent Ribbon control together with Prism (aka Composite WPF). Our team is using this combination for some time already; although, with various degrees of success: the Fluent Ribbon is not enough mature (but pretty stable) and my personal relation with Prism is kind of love/hate. The Castle Winsor is starring as IoC Container of choice.

The code is not something extraordinary by any means. You can find lots of similar samples for other controls in the net. It’s just a sample how to use these libraries together.

In this example I’ve set next scenario: a module (or another part of application) wants to add new RibbonTabItem to existing Ribbon without having a direct reference to it. Also, application should support activation/deactivation and removing of Tabs.

The basics are pretty straightforward: pick/write an IRegion, write an adapter to work as a mediator between it and control being extended, profit!..

I’ve decided to use an existing Region to serve as host for tabs – SingleActiveRegion. It seems to fit well expected behavior.

Two classes that I wrote are RibbonRegionAdapter and RibbonRegionSyncBehavior. The behavior class is used to synchronize the Tabs collection of the Ribbon with Views and ActiveViews collections of the SingleActiveRegion. Adapter serves only to instantiate the region and attach the behavior.

Also, this solution uses Castle Windsor to wire up all things. The WindsorBootstrapper class wires up an application based on Prism v2.2 and Castle.

I’ll not publish the the code here, you can “git clone …” it from repository on GitHub. Or, from the same place, you have the option to download sources.

Future plans include few more regions (Backstage, RibbonTabItem) and may be some tests.

Solution specifics:

  • Prism v2.2
  • Fluent Ribbon rev. 51299
  • Castle Windsor 2.1.1
  • VS 2010 & NET 4.0

Disclaimer: code is provided “AS IS”. It may contain bugs and not be written in best possible way. I’m not a Prism specialist by any means. Any help is welcome if somebody can improve this code or wants to contribute for a more meaningful example…

… more a note for myself

Found a discussion that gives a few very nice examples of WPF applied in real world, line-of-business applications: WPF LOB samples.

I can’t stop be amazed by what degree of flexibility WPF is giving to designers and application developers. Having an artistic sense and willingness to create “something” you can do really nice, usable & nice looking applications.

 

Update: reviewed some of my notes, found another great LOB sample, not listed in previous discussion: Great User Experience Example in a Business Application.

Update: Seen on Davy Brion‘s blog: How should your business applications look like. Few very interesting ideas there.

Running with scissors

Yesterday I have participated at my second meeting of ALT.NET community in Switzerland. The ALT.NET Swiss group was running for some time, but I’ve discovered it recently, after being contacted by Frédéric SCHÄFER from OCTO Technology. He started the group and has been organizing meetings in Lausanne and Geneva. Many thanks Frédéric and keep up a good job!

First meeting I attended was about WPF and Model-View-ViewModel pattern implementation. It was held by Frédéric SCHÄFER. He took a simple WPF application and slowly modified it to use MVVM pattern. That got us better separation of concerns and maid application more testable.

The yesterday’s meeting was for me a special one, I was one of the speakers.

I’ve talked briefly about how the MVVM pattern is used with WPF, showed a "classical" implementation of it and after that presented some improvements over that. The initial ViewModel had INotifyPropertyChanged  implemented and used ICommand to respond to UI actions initiated by view. For two properties and a command there was a lot of "ceremony" – code being there just to comply with implementation requirements. At the end of the session, by using two frameworks(BLToolkit and Caliburn) , I’ve managed to reduce the volume of the code by the half while maintaining initial functionality. Why is this important? Shorter code means less place to make a mistake, hide a bug, it’s faster to read and understand. I will write a separate blog post about my solution.

The other speaker was Daniel Vaughan, the author of Calcium SDK. He spoke about Calcium SDK, which is a set of additional services and modules built on top of the Prism (Composite WPF) and few Visual Studio templates to make creation of composite application easier. After that, he talked about compile time validation of WPF’s binding expressions. This, being a real headache for WPF developers, was addressed by Daniel by using template engine backed into Visual Studio – T4. His solution is very interesting. He generates some metadata classes with static properties and after that uses them in binding expressions. I’m still not sold on the whole idea, but I should spend some time playing with it. But presentation was nice, thanks Daniel!

It was a great evening. It’s always interesting to meet people with different set of skills and experience, you can look at known problems from other angles, getting suggestions or criticism. Or you just can learn something new.

Special thanks to Atif Aziz and Cargill International SA for hosting the session.

Update: published the code on GitHub.

Having to group a collection shown in UI by a command fired from the View, I’ve came to a nice solution by writing an extension method to the ObservableCollection<T> class.

The extension method:

public static class ObservableCollectionExtensions
{
    public static void VisualGroupingBy<T>(this ObservableCollection<T> collection,
                                           Expression<Func<T, object>> groupByExpresson)
    {
        var view = CollectionViewSource.GetDefaultView(collection);
        collection.VisualGroupingClear();

        var propertyName = ReflectionHelper.GetPropertyName(groupByExpresson);

        view.GroupDescriptions.Add(new PropertyGroupDescription(propertyName));
    }

    public static void VisualGroupingClear<T>(this ObservableCollection<T> collection)
    {
        var view = CollectionViewSource.GetDefaultView(collection);
        view.GroupDescriptions.Clear();
    }
}

 

How to use it:

public ObservableCollection<Address> Addresses { get; set; }
public void GroupAddressesByCity()
{
    Addresses.VisualGroupingBy(address => address.City);
}

Clean, no magic strings, readable…