Thursday, December 2, 2010

Using the Office 2010 Ribbon UI in WPF applications – Part 1

Microsoft released the Office UI licensing program for software developers who wish to implement the Office UI as a software component and/or incorporate the Office UI into their own applications. You can use the ribbon control in your WPF/ Silverlight apps to get all the features of office 2010 UI ribbon control in your applications. We'll see some of these features and samples to create a similar UI in a WPF application. I have used MVVM light as an MVVM framework in my samples.
The UI ribbon control is available for free download from this link. Once you have downloaded the source code or binaries, add a reference to the RibbonControlsLibrary to your WPF project. You can now use the controls that are part of this library in your application to create the UI. In this post we'll see how to use the ribbon control and create an Application menu in a WPF application.
In your XAML code add a new DockPanel and a ribbon control with Dock=Top to dock the ribbon to the top of the window.
<Window x:Class="Office2010RibbonUI.Samples.OfficeUISampleView"
        Title="Office2010 UI"
        Height="500" ResizeMode="NoResize"
        DataContext="{Binding [OfficeUISample], Source={StaticResource Locator}}" Icon="/Office2010RibbonUI.Samples;component/Images/Santa-Claus.png">

    <DockPanel LastChildFill="True">
        <my:Ribbon Height="100" DockPanel.Dock="Top">
        <Grid DockPanel.Dock="Bottom">
Next add application menu items to the ribbon control.
<my:Ribbon Height="100" DockPanel.Dock="Top">
        <my:RibbonApplicationMenu SmallImageSource="/Office2010RibbonUI.Samples;component/Images/Sphere_Glossy.png" AllowDrop="True" KeyTip="C" Height="25" Width="40">
            <my:RibbonApplicationMenuItem ImageSource="/Office2010RibbonUI.Samples;component/Images/Santa%27s-Hat.png"
                                            Command="{Binding XmasCapCommand}" Width="140" Header="XMas Cap"
                                            Margin="10 0 0 0"/>           
            <my:RibbonApplicationMenuItem ImageSource="/Office2010RibbonUI.Samples;component/Images/Rudolph.png"
                                            Command="{Binding RudolphCommand}" Width="140" Header="Rudolph"
                                            Margin="10 0 0 0"/>
            <my:RibbonSeparator />
            <my:RibbonApplicationMenuItem ImageSource="/Office2010RibbonUI.Samples;component/Images/SantasBag.png"
                                            Command="{Binding SantasBagCommand}" Width="140" Header="Santas Bag"
                                            Margin="10 0 0 0"/>

You can also add split menu items to the main group.
If you also use the code given below to show items in the auxiliary panel of the application menu.
            <RowDefinition Height="20" />
            <RowDefinition Height="*" />
        <my:RibbonSeparator Label="Recent Commands" />
        <ItemsControl Grid.Row="1" ItemsSource="{Binding RecentCommands}">
                    <TextBlock Text="{Binding}" Margin="5" />
 My View model for the view looks like
public class OfficeUISampleViewModel : ViewModelBase
    public OfficeUISampleViewModel() { }

    public ICommand XmasCapCommand
            if (_xmasCapCommand == null)
                _xmasCapCommand = new RelayCommand(() => MessageBox.Show("XMas command executing..."));
            return _xmasCapCommand;

    public ObservableCollection<string> RecentCommands
            return new ObservableCollection<string> { "Santas Bag", "Rudolp" };

    ICommand _xmasCapCommand;

Next post we’ll see how to add ribbon tabs and items to the ribbon control

No comments: