Code behind

Mar 18, 2010 at 6:40 AM

Can you provide me sample for creating ribbon window with some tabs and ribbon buttons/ribbon dropdown in code behind??

Mar 18, 2010 at 10:13 AM

I am using below code in RibbonWidow.xaml.cs file to create a ribbon command and ribbon group in code behind.

but the buttons are not enabled. please correct where I am wrong?

        public void CreateRibbon()
        {
            Fluent.Ribbon ribbon = new Fluent.Ribbon();
            ribbon.Name = "ribbon";
            Fluent.RibbonTabItem ti= new RibbonTabItem();
            ti.Header = "Home";
            ribbon.Tabs.Add(ti);
            //this.Content = ribbon;           
            Fluent.RibbonGroupBox group = new RibbonGroupBox();
            group.Header = "Test Group";
            group.ToolTip = "Group Tool Tip testing";
            group.IsLauncherVisible = false;
            group.IsEnabled = true;
            foreach (IPresenterCfgCommandItem item in Framework.PresenterCfgCommandItem)
            {
                group.Items.Add(CreateRibbonButton(item));
            }
            ti.Groups.Add(group);
            MainUI.Children.Add(ribbon);
        }

        public Fluent.Button CreateRibbonButton(ICustomCommand commanditem)
        {
            Fluent.Button button= new Fluent.Button();
            button.Command = commanditem.ItemCommand;
            button.Text = commanditem.ItemName;
            button.ToolTip = commanditem.ItemTooltip;
            button.CommandBindings.Add(commanditem.ItemCommandBinding);
            button.IsEnabled = true;
            return button;
        }

Buttons are displayed in the window but they are disabled.

Coordinator
Mar 18, 2010 at 11:04 AM

Hello, rmohanraj.

I suppose that your command is disabled at this moment (ICommand.CanExecute == false) or CanExecuteChanged doesn't occured after CanExecute becomes true. When you use Command, IsEnabled is ignored.

Daniel

Mar 18, 2010 at 11:08 AM

Hello Daniel,

Thanks for your reply. Command.CanExecute is set to true. i am not sure whether CanExecuetChanged is fired or not. I am not sure how to check CanExecuteChanged fired or not.

Regards,

Mohan

Coordinator
Mar 18, 2010 at 11:25 AM
Edited Mar 18, 2010 at 11:26 AM

I have just tried the following command:

    public class FooCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        
        bool canExecute = true;
        public bool CanExecute(object parameter)
        {
            return canExecute;
        }
        
        public void Execute(object parameter)
        {
            MessageBox.Show("Foo Command is Executed");
        }

        public FooCommand()
        {
            DispatcherTimer timer = new DispatcherTimer(TimeSpan.FromSeconds(1.5), DispatcherPriority.Normal, OnTick, Dispatcher.CurrentDispatcher);
        }

        void OnTick(object sender, EventArgs e)
        {
            canExecute = !canExecute;
            if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);
        }
    }

And slightly changed you method:

public Fluent.Button CreateRibbonButton()
{
       FooCommand fooCommand = new FooCommand();

       Fluent.Button button = new Fluent.Button();
       button.Command = fooCommand;
       button.Text = "Text";
       button.ToolTip = "ToolTip";
       //button.CommandBindings.Add(commanditem.ItemCommandBinding);
       button.IsEnabled = true;
       return button;
}

Does this command work in your environment?

Mar 18, 2010 at 12:27 PM

You are right, When i use your command it is working fine. when i try to use command bindings (and setting CanExecute true in the CommandBinding it is not responding as expected fro Ribbon Button, where as same works fine for WPF button.)

  
    public class FooCommand1
    {

        public static RoutedCommand TestPresnterCommand = new RoutedCommand("TestPresnterCommand", typeof(MainFramework));

        public System.Windows.Input.ICommand ItemCommand
        {
            get { return TestPresnterCommand; }
        }

        public System.Windows.Input.CommandBinding ItemCommandBinding
        {
            get { return new CommandBinding(TestPresnterCommand, OnTestCommandExecuted, CanExecuteTestCommand); }
        }

        public FooCommand1()
        {

        }


        private void CanExecuteTestCommand(object sender,
       CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void OnTestCommandExecuted(object sender,
        ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Test Module Command");
        }

    }


        public Fluent.Button CreateRibbonButton(IPresenterCfgCommandItem commanditem)
        {
            Fluent.Button button= new Fluent.Button();
            FooCommand1 fooCommand1 = new FooCommand1();
            button.Command = fooCommand1.ItemCommand;
            button.Command = commanditem.ItemCommand;
            button.Name = commanditem.ItemName;
            button.ToolTip = commanditem.ItemTooltip;
            button.CommandBindings.Add(fooCommand1.ItemCommandBinding);
            return button;
}

where as in CreateRibbonButton, when i replace Fluent.Button with System.Windows.Controls.Button. button is enabled and responding to the event. Please suggest me if there is any issues.
Mar 19, 2010 at 11:31 AM

Hello Daniel,

Did you get any chance to look into this. I thought there could be some problem in CommandBindings in RibbonButton. Please update me.

Regards,

Mohan

Coordinator
Mar 19, 2010 at 1:08 PM
Edited Mar 19, 2010 at 1:25 PM

Hello, Mohan.

I hadn't time yet to investigate this issue properly. Our Fluent.Button based on System.Windows.Control. We do not override CommandBindings. Maybe we have logical tree issues...
I can suggest you workaround, you can replace the line:

button.CommandBindings.Add(fooCommand1.ItemCommandBinding);

with:

window.CommandBindings.Add(fooCommand1.ItemCommandBinding);

(where window is Fluent.RibbonWindow or System.Windows.Window)

Daniel

Apr 10, 2010 at 2:58 AM

Hi Daniel,Great work ! I just found the code behind session and wanted to join my quesion into this group. Could you please give me a code behind example of QuickAccessMenuItem ,  I added buttons to it,but it didn't show. Also I am wondering how can I add shortcuts of the buttons in RibbonTabItems to the QickAccessMenu(code behind), thanks!

Kevin

 

Coordinator
Apr 10, 2010 at 4:06 AM
Edited Apr 10, 2010 at 4:08 AM

Hello, Kevin.

You can add pinned Quick Access Items named QuickAccessMenuItem to collection Ribbon.QuickAccessItems. To associate target element you may bind it to QuickAccessMenuItem.Target property or just set the content.

Fluent.Button button = new Fluent.Button();
button.Text = "Text";
button.Icon = <Your Image>;

Fluent.QuickAccessMenuItem qatItem = new Fluent.QuickAccessMenuItem();
qatItem.Target = button; // Here you can set one of your controls from a ribbon's tab as well
qatItem.IsChecked = true;
ribbon.QuickAccessItems.Add(qatItem);

* I haven't tried to compile this code snipped

Daniel

Apr 12, 2010 at 6:45 AM

Daniel,

It works!!! Thanks!

Kevin

Oct 3, 2011 at 2:46 PM

Hi there,

I was able to add the ribbon menu as per the fist post and also the groups and buttons. However i couldn't add the RibbonMenu. Here's what i tried but no luck. Can someone please give some guidance?

        Dim menu As New Fluent.RibbonMenu
        Dim backStage As New Backstage
        'backStage.Background = New BrushConverter().ConvertFromString("Blue")
        Dim backStageTabControl As New BackstageTabControl
        backStageTabControl.Items.Add(button3)


        'menu.Items.Add(backStage)
        ribbon.Menu = menu
Thanks in advance.