user1883843
user1883843

Reputation: 3

Exception fire when trying to open the screen

When I am trying to open the screen I got the following exception:

Unable to cast object of type 'System.Windows.Controls.Grid' to type 'System.Windows.Controls.TabItem'

Any help will be appreciated.

partial void VouchersDetail_Created()
    {
        this.FindControl("JournalVoucher").ControlAvailable += JournalVoucher_ControlAvailable;
    }

    void JournalVoucher_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
        ((System.Windows.Controls.TabItem)e.Control).KeyUp += JournalVoucher_KeyUp;
    }

    void JournalVoucher_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.V)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                var tabitem = (System.Windows.Controls.TabItem)sender;
                tabitem.IsEnabled = true;
            }
        }
    }

Thanks

Upvotes: 0

Views: 62

Answers (2)

Viktor
Viktor

Reputation: 699

My guess is that JournalVoucher is of type Grid. And you are trying to cast that to the type TabItem.

Put a breakpoint on the line and check if the event gets called more than once.

If it only gets called once than it may be enough to change the line

((System.Windows.Controls.TabItem)e.Control).KeyUp += JournalVoucher_KeyUp;

to:

((System.Windows.Controls.Grid)e.Control).KeyUp += JournalVoucher_KeyUp;

Most likely the Sender in JournalVoucher_KeyUp is also of type Grid

Upvotes: 1

ColinE
ColinE

Reputation: 70170

There are a number of lines in your code where you try to cast an object to TabItem:

var tabitem = (System.Windows.Controls.TabItem)sender;

One of these will no doubt be the root cause!

Your code is mostly event handlers - my guess is that these are defined on a Grid.

Upvotes: 0

Related Questions