.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

stuck in Ribbonbutton.CommandHander , i have created wpf UI

10 REPLIES 10
Reply
Message 1 of 11
Aravinth.Pichamani
284 Views, 10 Replies

stuck in Ribbonbutton.CommandHander , i have created wpf UI

using Autodesk.AutoCAD.ApplicationServices;
using System;
using System.Windows.Controls.Ribbon;
using System.Windows.Input;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace LabelsCross
{
    public class RibbonCommandHandler : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;

            if (parameter is RibbonButton)
            {// here what i neeed do
                Application.ShowModelessWindow(?);
            }
        }
    }
}
10 REPLIES 10
Message 2 of 11

AravinthPichamani_0-1736083780637.png

this my WPF code

Message 3 of 11

Are you familiar with how to create a Window like the one you show?

 

You need to create an instance of the Window and pass it as the argument to ShowModalWindow().

Message 4 of 11

I'm familiar with WPF. I'm created above show picture. But when I'm clicking button not showing windows (wpf)

Message 5 of 11

You are not showing the relevant code.

 

In the code you showed you have a question mark as the argument. What are you passing as that argument?

Message 6 of 11


@Aravinth.Pichamani wrote:

I'm familiar with WPF. I'm created above show picture. But when I'm clicking button not showing windows (wpf)


Which button you click? Since you showed the code of RibbonCommandHandler, I assume the RibbonCommandHandler is SUPPOSED to be assigned to a RibbonCommandItem (typically a RibbonButton) created by your code. So, does your code creates a such RibbonCommandItem? If yes, show the code. If not, I do not see the code of RibbonCommandHandler is relevent when it is not used anywhere. It might be too much for someone who is not yet very familiar to AutoCAD .NET API programming to figure out how to create a RibbonCommandItem in order to just show an UI in AutoCAD. You should begin with creating a simply CommandMethod to show either "Hello AutoCAD" at command line, or show a UI:

 

[CommandMethod("ShowMyUI")]

public static void ShowMyMainWindow()

{

   var win=new MainWindow()

   win.DataContext=MyMainViewModel;  // If you do know WPF well, you should do MVVM

   var result = Application.ShowModalWindow(win);

   if (result.HasValue && result.Value)

   {

      // If the modal UI is oked by user

      // you might want to do something based on the user inputs in the UI

   }

}

 

With this command "ShowMyUI" available, and you indeed created a RibbonCommandItem, which is assigned the RibbonCommandHandler, then you can set the RibbonCommandItem's CommandParameter property with this command, like

 

MyRibbonButton.CommandHandler = RibbonCommandHandler;

MyRibbonButton.CommandParameter = "ShowMyUI ";

...

 

Then in the RibbonCommandHandler, you can:

 

 

   ...
        public void Execute(object parameter)
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;

            if (parameter is RibbonButton)
            {
                var commandString=((RibbonButton)parameter).CommandParameter.ToString();
                var dwg=Application.DocumentManager.MdiActiveDocument;
                if (dwg != null)
                {
                   dwg.SendStringToExecute(commandString, true, false, false);
                }
            }
        }
   ...

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 11

    public class EApplication : IExtensionApplication
    {

        public static class Images
        {
            public static BitmapImage GetBitmap(string path)
            {
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.UriSource = new Uri(path);
                bmp.EndInit();
                return bmp;
            }
        }
        
        public static void CreateRibbonbtn()
        {
            RibbonControl ribbon = ComponentManager.Ribbon;

            RibbonTab tab = new RibbonTab();

            // setting the name for the tab
            tab.Title = " LD & ME";
            // settting the id for the tab
            tab.Id = "LD";

            ribbon.Tabs.Add(tab);

            // creating a new panelsource for the tab

            RibbonPanelSource panelSource = new RibbonPanelSource();
            panelSource.Title = "Label";

            // creating a panel from the source 

            RibbonPanel panell = new RibbonPanel();
            panell.Source = panelSource;

            // adding the panel to the tabs we created 
            tab.Panels.Add(panell);

            //creating a button for the panel we have created

            try 
            {
                RibbonButton btn = new RibbonButton();
                btn.Text = "Lable";
                btn.ShowText = false;
                btn.CommandParameter = "";
                btn.ShowImage = true;
                btn.Image = Images.GetBitmap(@"C:\WSP\code\civil 3d c#\LabelsCross\LabelsCross\Resources\pipe.png");
                btn.Orientation = System.Windows.Controls.Orientation.Vertical;
                btn.CommandHandler = new RibbonCommandHandler();
                panelSource.Items.Add(btn);
                btn.Size = (RibbonItemSize)250;
            }

            catch (System.Exception ex) 
            {
                Application.ShowAlertDialog(ex.Message);
            }
       
           
        
        }

        public void Initialize()
        {
            CreateRibbonbtn();
            //throw new NotImplementedException();
        }

        public void Terminate()
        {
            //throw new NotImplementedException();
        }
    }
Tags (1)
Message 8 of 11

AravinthPichamani_0-1736135414247.png

 

Hi Yuan, I created my first button and WPF UI. Next, I need to develop the MVVM pattern. Above, I pasted the ribbon button code. Could you please help me? when I'm clicking that button window not showing.

Tags (1)
Message 9 of 11

If you read my reply carefully, you should have seen this

<QUOTE>

With this command "ShowMyUI" available, and you indeed created a RibbonCommandItem, which is assigned the RibbonCommandHandler, then you can set the RibbonCommandItem's CommandParameter property with this command, like

 

MyRibbonButton.CommandHandler = RibbonCommandHandler;

MyRibbonButton.CommandParameter = "ShowMyUI ";

</QUOTE>

 

That is, in your code, simply add 1 line code:

 

            try 
            {
                RibbonButton btn = new RibbonButton();
                btn.Text = "Lable";
                btn.ShowText = false;
                btn.CommandParameter = "";
                btn.ShowImage = true;
                btn.Image = Images.GetBitmap(@"C:\WSP\code\civil 3d c#\LabelsCross\LabelsCross\Resources\pipe.png");
                btn.Orientation = System.Windows.Controls.Orientation.Vertical;
                btn.CommandHandler = new RibbonCommandHandler();
                // Set CommandParameter to an existing command,
                // be it a built-in one, or your own command
                btn.CommandParameter = "ShowMyUI " 
                panelSource.Items.Add(btn);
                btn.Size = (RibbonItemSize)250;
            }

            catch (System.Exception ex) 
            {
                Application.ShowAlertDialog(ex.Message);
            }

 

 

Of course, you need to update your RibbonCommandHandler code as I showed in my previous reply and you would have a CommandMethod that defines the corresponding command "ShowMyUI", also showed in my previous reply.

 

Also, I assume your code is just concept-proving code. That is, in real world, you would not run the CreateRibbonBtn() method directly in the IExtensionApplication.Intialize(). This has been discussed in this forum many times, years. You can search "Ribbon" in this forum for the past discussion, and especially pay attention to the answers provided by @ActivistInvestor 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 10 of 11

Hi yuan i tried what you are showing the code still not working 

Message 11 of 11

There is nothing I can tell without see all the code. I only know you have a RibbonButton available. So, the first question is: did you debug the ribbonbutton click by placing a break point in the RibbonCommandHandler? If the break point is hit, you should clearly see what the issue is, such as whether the command assigned to the CommandParameter is executed by Document.SendStringToExecute().

 

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report