views:

84

answers:

1

Hi there. I'm using the Microsofts RibbonControlLibrary for a Ribbonmenu with several RibbonCommands. I have a TabControl that should contain tabs for each ribbon button pressed. E.g. I click on RibbonCommand 2, I want a tab called "Tab2" be created, if it is not already present in the TabControl, otherwise it should be focused.

I've already implemented http://www.codeproject.com/KB/WPF/WpfTabCloseButton.aspx, which are very nice.

I think I still don't get the event mechanism of WPF quite right. My code strongly adapted the sample project of the Ribboncontrol.

This is how I implemented the Command:

<Window.CommandBindings>
  <CommandBinding Command="me:AppCommands.Protokoll" Executed="RibbonButton_Click" /> 
</Window.CommandBindings>

The button inside the RibbonControl looks like the following.

<r:RibbonButton Command="me:AppCommands.Protokoll" />

AppCommands.cs

public class AppCommands
{
  public static RibbonCommand Protokoll
  {
     get { return (RibbonCommand)Application.Current.Resources["ProtokollCommand"]; }
   }
}

ResourceDictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary">
    <r:RibbonCommand x:Key="ProtokollCommand"                
        LabelTitle="foo"
        ToolTipTitle="foo"
        ToolTipDescription="foo" />
</ResourceDictionary>

What I now want to write is a Method that creates a Tab for each button click, where as the created tabpage contains specific usercontrols. I do not know, how to best implement the mapping between the CommandButtons and the Tabs. I think its best to use just one click-eventhandler rather than a eventhandler for each button. Questions:

1) How can I find out which unique button raised the event? I've only managed to get the information in the ResourceDictionary. Unfortunatly there is no such property as uniqueID or something like this. I've done this with:

private void RibbonButton_Click(object sender, RoutedEventArgs e)
{
  // ((Microsoft.Windows.Controls.Ribbon.RibbonCommand)(((System.Windows.Controls.Button)(e.OriginalSource)).Command))
}

I do not want to create a mapping based on a property like LabelTitle that is supposed for display.

2) I've found this http://stackoverflow.com/questions/2277464/wpf-c-how-does-one-reference-tabitems-inside-a-tabcontrol which showed me how to query the Items collection of the TabControl. Is that a good attempt or is there a better one?

3)
How should I create the mapping between the RibbonCommand and the TabPage + Controls that should be displayed. Of course I could use a Dictionary like

Dictionary<String, fooContainer>
// ...
public class fooContainer()
{
  public String TabHeaderString {get;set;}
 // other properties
}

Or does WPF present a better approach with these ResourceDictionaries, that I haven't understood till now.

+1  A: 

Since you are using a Command to manage the tabs, I suppose you could set the CommandParameter property of each Button in a meaningful way, so that it is possible to discriminate among the different actions you need to achieve (i.e. it could be an Enum, if you already know the kind of tabs you need to expose, or a more complex object in case such tabs can be created dynamically).

By the way, the signature of the RibbonButton_Click method doesn't look correct to me, since the arguments passed to the method should be of type ExecutedRoutedEventHandler. Doing so will allow you to access the CommandParameter specified at the Button level, through the Parameter property of the args variable. Following the code to modify:

1. Add a proper CommandParameter to your RibbonButton

<r:RibbonButton Command="me:AppCommands.Protokoll" CommandParameter="TabKind.Sample1"/>
<r:RibbonButton Command="me:AppCommands.Protokoll" CommandParameter="TabKind.Sample2"/>

2. Change the handler to the Execute event, to match a proper signature, and handle the parameter according to your needs

private void RibbonButton_Click(object sender, ExecutedRoutedEventArgs e)
{
  TabKind kind = (TabKind)e.Parameter;
  switch (kind)
  {
       case TabKind.Sample1:
       {
           //Do something meaningful...
           break;
       }
       case TabKind.Sample2:
       {
           //Do something meaningful...
           break;
       }
  }
}

To answer you questions directly:

1. You shouldn't need to know which instance of the button raised the Command, the proper way is to use a CommandParameter to identify what kind of action you want to perform (the RibbonButton is just the host of the Command, doesn't make senso to me to design the logic of the application to the specific UI implementation).

2. If you really need to query the TacControl for its TabItemchildren, the link you provided is of course the way to go.

3. Once you have identified the action you need to perform, you could retrieve the instance of the TabItem you need through a register you previously created (i.e. you can have a proper Dictionary used to associate the Enum value with a TabItem instance). Another possible way, is to use the Tag property on each TabItem, set it to a proper Enum value and use the following method to retrieve the proper TabItem (note that kind is the variable defined in the previous snippet):

TabItem matchingItem = tab_main.Items.Cast<TabItem>()
                                     .Where(item => item.Tag == kind)
                                     .FirstOrDefault();
BladeWise