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 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.
Since you are using a
Commandto manage the tabs, I suppose you could set theCommandParameterproperty of eachButtonin a meaningful way, so that it is possible to discriminate among the different actions you need to achieve (i.e. it could be anEnum, 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_Clickmethod doesn’t look correct to me, since the arguments passed to the method should be of typeExecutedRoutedEventHandler. Doing so will allow you to access theCommandParameterspecified at theButtonlevel, through theParameterproperty of theargsvariable.Following the code to modify:
1. Add a proper
CommandParameterto yourRibbonButton2. Change the handler to the
Executeevent, to match a proper signature, and handle the parameter according to your needsTo 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 aCommandParameterto identify what kind of action you want to perform (theRibbonButtonis just the host of theCommand, 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
TacControlfor itsTabItemchildren, 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
TabItemyou need through a register you previously created (i.e. you can have a properDictionaryused to associate theEnumvalue with aTabIteminstance). Another possible way, is to use theTagproperty on eachTabItem, set it to a properEnumvalue and use the following method to retrieve the properTabItem(note thatkindis the variable defined in the previous snippet):