Load ribbon tab at run time

Aug 9, 2011 at 1:42 PM

Hi,

i have been experimenting with this ribbon control. Excellent work.

I am wondering how I can load a Tab at runtime.

In my experiment I have a page with the ribbon control (I add a default Home Tab in the Markup which displays fine) and a button. In the button Click handler, I new up a tab, give it a Title="TestTab", then add it to the Ribbon.Tabs collection. However the Tab does not show up in the UI.

If I click my button again and look into the Ribbon.Tabs collection, I can see both Tabs there, but the 'HasLoaded' property is false for the TestTab, and True for the 'Home' Tab I added in the Mark-up

How do I get the Ribbon to load all tabs in it's tab collection?

Any pointers would be a ppreciated.

Thanks

John.

Aug 9, 2011 at 5:16 PM

As the code exists currently, simply adding a control to the collection does not add the control into the UIElement hierarchy. If you want the UI to actually reflect the current contents of the Tabs collection, you’ll need to modify the code. Probably replace the Tabs collection with a custom collection you write that overrides (or implements) the Add and Remove methods, firing off events which you then add handlers to which force a redraw.

Otherwise, you’ll need to redraw the entire control. I’m not sure that simply calling Invalidate will take care of it. If it doesn’t, since I haven’t been in that code in a good while and can’t give you any quick pointers, I’d suggest looking through it and following the logic. Then you could add a public Redraw method that would remove all child UI elements and redraw from scratch.

Hope that helps a little.

Ben

From: ltheONEl [email removed]
Sent: Tuesday, August 09, 2011 9:43 AM
To: Coats, Benjamin
Subject: Load ribbon tab at run time [silverlightribbon:268375]

From: ltheONEl

Hi,

i have been experimenting with this ribbon control. Excellent work.

I am wondering how I can load a Tab at runtime.

In my experiment I have a page with the ribbon control (I add a default Home Tab in the Markup which displays fine) and a button. In the button Click handler, I new up a tab, give it a Title="TestTab", then add it to the Ribbon.Tabs collection. However the Tab does not show up in the UI.

If I click my button again and look into the Ribbon.Tabs collection, I can see both Tabs there, but the 'HasLoaded' property is false for the TestTab, and True for the 'Home' Tab I added in the Mark-up

How do I get the Ribbon to load all tabs in it's tab collection?

Any pointers would be a ppreciated.

Thanks

John.

Aug 10, 2011 at 5:57 PM

Thanks for your quick response, it helps.

I have a quick an dirty implemntation which I have palced in the Tabs_CollectionChanged method of the main Ribbon.cs - That works for adding tabs, But though I store the Column definition index in the Tab.Tag, and I can remove the tab, something goes a bit strange here and I am left with a ghost tab, Or if the Tab I am removing is the current selected one the ribbon implodes and I get Null reference exceptions. I am going play with this code till I understand exactly how it builds it's self and see if I can solve my issue.

Failing that plan B: I will extract the code from the OnApplyTemplate method which is responsible for loading the tabs and call it RefreshTabs or similar, That way when I add Tabs to the collection I can call RefreshTabs and have the control redraw all tabs. 

I'm a bit of a silverlight novice (you may have guessed :) ) I had experimented with custom controls, but thanks to your awsome work, I think I am begining to understand things more.

When I get this working, I may post here, what I did, for the sake of other novices who may find and use this control.

Again thanks for your answer.

John

Aug 11, 2011 at 3:14 PM

Hi Ben,

There are many things I dont understand! however there is one thing regarding tabs I can't figure out (yet)

Forgetting that I am trying to add tabs at runtime for a moment.

When I add the ribbon control to a page, and in the xaml I add tabs.  Those tabs described in the page xaml get injected into the ribbon Tabs collection. but I can't see how this is done.

<page>
         <Ribbon>
                   <ribbonTab>
                   <ribbonTab>
                   <ribbonTab>
          </Ribbon>
</Page>

Those tabs are being added to the ribbon control Childern, right?

So how do the Ribbon Control Children end up in the Ribbon.Tabs collection???

When I debug the TabCollection_Changed method, I find that the call comes from external code (according to Visual Studio)

I know that the Tabs collection is populated before the Ribbon.OnApplyTemplate is called. I just can't figure out how.

I get fraustrated when I can't figure stuff out. So any pointers you may offer will be greatly appreciated.  I feel understanding this will help me understand better custom Controls in general.

Thanks again

John.

Aug 11, 2011 at 3:31 PM

Ahh... It that [contentProperty("Tabs")] Attribute isn't it!

Doh!