Open Wpf Window , but Keyboard does not work

118 views
Skip to first unread message

Glayson Patricio

unread,
Oct 5, 2022, 9:29:08 PM10/5/22
to Excel-DNA
I created a Ribbon that has some buttons that open wpf windows.

I managed to make the windows open using the code below

    public void RibbonAction(IRibbonControl control)
    {
        if (Application.Current is null)
        {
            using var scope = AssemblyLoadContext.EnterContextualReflection(typeof(...TypeFromMyWpfLibrary...).Assembly);            
            var application = new Application();
            ... some code to load wpf resource dictionaries, etc..
        }

        .... some code to create window content based on Button pressed on ribbon
        var w = new Window();
        w.Content = content;
        w.Show();
    }

Everything seems to work fine, but the KEYBORAD does not respond to the WPF window.
If i press the keyboard, i am redirected to an excel cell, that is filled with the key I pressed.


I tryed solving it by putting "Application.Current.Run()" below "w.Show()"

But i reversed things.....
When I do it, the excel does not respond anymore to the keyboard.


Is there a better way to open those wpf windows ?
How could i manage that keyboard situation ?

Every help is appreciated,

Glayson

Glayson Patricio

unread,
Oct 5, 2022, 10:26:35 PM10/5/22
to Excel-DNA
Could make it work.... folow the code



    public void RibbonAction(IRibbonControl control)
    {
        void ShowWindow()
        {
            .... create window and content....
            w.Show();
        }

        if (Application.Current is null)
        {
            Thread thread = new(x => {
                using var scope = AssemblyLoadContext.EnterContextualReflection(typeof(...sometype....).Assembly);

                var application = new Application();
                .... configure application resources.........
                ShowWindow();
                Application.Current.Run();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
        }
        else
        {
            Application.Current.Dispatcher.Invoke(ShowWindow);

John Smith

unread,
Mar 17, 2023, 7:42:57 AM3/17/23
to Excel-DNA
Hi Glayson Patricio,


I am trying to do this but I could not make it work..
I've created a ribbon with some buttons and when I click on those buttons I would like to open a new window with some inputs to perform some calculations with those values.
Could you please share a sample project, a basic one, on github or here please?

Many thanks!!


Best regards,
John

A Parmar

unread,
Apr 23, 2024, 5:15:06 AM (2 days ago) Apr 23
to Excel-DNA
Hi,

was there a solution to this one?

Brian Satola

unread,
Apr 23, 2024, 7:09:38 AM (2 days ago) Apr 23
to Excel-DNA
Reply all
Reply to author
Forward
0 new messages