Remebering items added to QAT when application restarts

Ribbon for WPF Forum

Posted 16 years ago by Anurodh Ora
Version: 4.5.0483
Avatar
Hi,

I would like to know how can we implement the feature of remebering items added to QAT at runtime. How to get the list of QAT items?

Scenario is that, we can add ribbon menu items in the QAT by right click on the ribbon button at run time. Now, If we close the application and reopen it then the items which were added last time in the QAT should be available.

Could you suggest how can we implement this feature.

Thanks and Regards,
Anurodh

Comments (8)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Anurodh,

Please look at the "Customizing the QAT" QuickStart since it shows how to implement save/load QAT state functionality. You can persist the state XML string however you like.


Actipro Software Support

Posted 16 years ago by Anurodh Ora
Avatar
Thanks for the reply.

However looking at the sample we could not get any state xml string defined.
Could you please post some code to get/set state xml string.

Thanks,
Anurodh
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Anurodh,

It's the first line in the MainControl.OnSaveStateButtonClick method of the CustomizeQat sample that shows how to save it. The first line of OnLoadStateButtonClick shows how to reload it.


Actipro Software Support

Posted 16 years ago by Anurodh Ora
Avatar
Thanks for the reply.
Posted 16 years ago by Anurodh Ora
Avatar
Hi,

I got an issue with the saving of QAT items in case of command binding and it can be reproduced by altering the CustomizeQAT sample as below:

Edit MainControl.xaml of CustomizingQAT folder as :

1) Insert in UserControl tag
xmlns:local="clr-namespace:ActiproSoftware.Windows.ProductSamples.RibbonSamples.QuickStart.CustomizingQat"

2) Edit Save button xaml code of ribbon:Ribbon.QuickAccessToolBarItems tag as
<ribbon:Button Id="Save" ImageSourceSmall="/Resources/Images/Save16.png" Label="Save" Command="local:MainControl.cmdTest" />


Edit MainControl.xaml.cs of CustomizingQAT folder as :

1) Add below line of code in MainControl class

public static RoutedCommand cmdTest = new RoutedCommand();
2) Edit MainControl() constructor as :

public MainControl() {
            this.CommandBindings.Add(new CommandBinding(MainControl.cmdTest, cmdTest_Executed));
            InitializeComponent();
        }
3) Add this two private methods in the class

private void cmdTest_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Hello");
        }

        private void cmdTest_Enabled(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
Now run the application and click on "Customizing the QAT" option
1) Notice that the "Save" button in QAT is disabled.
2) Now click on the "Save QAT State" button and notice that Save in QAT get Enabled.
3) Click on Save and a messagebox of Hello appears and it confirms that the binding is proper.
4) Click on the "Save QAT State" button to save the QAT state (that has Save button in enable state)
5) Now click on "Load QAT State" button and notice that again the "Save" button in QAT is disabled.

So, the problem is when we load state why the Save button is disabled when it was enabled when we saved the state?

Please look into this issue.

Thanks in advance.

Anurodh
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Anurodh,

Any time you have problems with controls getting enabled/disabled and commands are involved, it's due to how WPF processes keyboard focus. The Ribbon is at its core the same as a normal WPF ToolBar, which would have the same command/focus issues.

Sometimes what happens when controls get added/removed in WPF, focus moves up to a high level in the control hierarchy and causes WPF's command architecture to reevaluate command handlers. In your case, although I didn't try it, my guess is that focus has moved "above" the MainControl level thus your command handler is not seen. Therefore WPF disables the button thinking there is nothing to handle it.

To resolve it, make sure that you keep focus down within MainControl after calling the Load QAT State button. That will most likely fix the problem.

Again this is not something specific to our control, it is just how the WPF command system works (and we do realize sometimes it can be frustrating). The same behavior would be exhibited by any control like ToolBar that also is a separate focus scope and can contain buttons with commands.

Hope that helps.


Actipro Software Support

Posted 16 years ago by Anurodh Ora
Avatar
Hi,

I tried setting focus to MainControl but it did not work. Could you please follow the steps presented above and see what is going wrong here.

We need to implement save/load QAT but this issue is delaying it. Please have a look into it and it may be the case that i may be doing something wrong here.

Thanks in advance,
Anurodh
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Ok I see the problem. If you define a Button with a Command directly in QuickAccessToolBarItems, when it serializes it, it serializes the actual RoutedCommand instance since you already basically assigned it an instance in XAML and after that, it doesn't know how to get to it again.

You can see this in the generated XAML for the QAT save state. When you deserialize it, it deserializes a new RoutedCommand instance that doesn't match your custom one.

So really what you would want to do is not define anything in QuickAccessToolBarItems directly in XAML. Instead, define your items in the Ribbon itself or QuickAccessToolBarCommonItems and then call CloneService.CreateClone and clone appropriate items in those locations and add them programmatically to the QuickAccessToolBarItems collection.

This way your serialized items will already be clones and should serialize with references to the real "source" objects, whose properties will be guaranteed to be properly bound. That should let everything reload correctly.

It seems that these steps aren't needed if you just use the built-in WPF commands since those serialize and deserialize references back to the framework ones ok. But if you use custom commands at all, you should probably do the above.


Actipro Software Support

The latest build of this product (v24.1.3) was released 27 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.