菜单项和菜单项的问题按钮事件

发布于 2024-12-28 20:53:23 字数 1774 浏览 1 评论 0原文

我正在尝试将用户控件作为页面传递。我让它与按钮一起工作。当我添加带有单击事件的菜单时,它不再起作用。

这是代码块,它确定将哪个 UserControl 填充到主布局中。这部分仅使用按钮并且确实有效。

private void btnGeneral_Click(object sender, RoutedEventArgs e)
    {
        PanelMainContent.Children.Clear();
        Button button = (Button)e.OriginalSource;
        PanelMainWrapper.Header = button.Content;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
    }

本部分尝试使用菜单项和按钮,但它不起作用

public void btnGeneral_Click(object sender, RoutedEventArgs e)
    {   

        PanelMainContent.Children.Clear();
        MenuItem menuItem = (MenuItem)e.OriginalSource;
        Button button = (Button)e.OriginalSource;

        if (e.OriginalSource == menuItem)
        {
            PanelMainWrapper.Header = menuItem.Header;
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            PanelMainContent.Children.Add(_userControls[menuItem.Tag.ToString()]);
        }

        if (e.OriginalSource == button)
        {
            PanelMainWrapper.Header = button.Content;
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
        }
    }

我收到错误。

XamlParseException:
The invocation of the constructor on type 'Test.MainWindow' that matches the specified    binding constraints threw an exception.' Line number '5' and line position '9'

InnerException
{"Unable to cast object of type 'System.Windows.Controls.Button' to type 'System.Windows.Controls.MenuItem'."}

任何指导将不胜感激。

谢谢!

I'm trying to pass user controls as pages. I have it working with buttons. When I added a menu with click events it no longer works.

This is the block of code where it figures out which UserControl to Fill into the main layout. This part only uses buttons and does work.

private void btnGeneral_Click(object sender, RoutedEventArgs e)
    {
        PanelMainContent.Children.Clear();
        Button button = (Button)e.OriginalSource;
        PanelMainWrapper.Header = button.Content;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
    }

This Part attempts to use the MenuItems and Buttons, It does not work

public void btnGeneral_Click(object sender, RoutedEventArgs e)
    {   

        PanelMainContent.Children.Clear();
        MenuItem menuItem = (MenuItem)e.OriginalSource;
        Button button = (Button)e.OriginalSource;

        if (e.OriginalSource == menuItem)
        {
            PanelMainWrapper.Header = menuItem.Header;
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            PanelMainContent.Children.Add(_userControls[menuItem.Tag.ToString()]);
        }

        if (e.OriginalSource == button)
        {
            PanelMainWrapper.Header = button.Content;
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
        }
    }

Error(s) I Recieve.

XamlParseException:
The invocation of the constructor on type 'Test.MainWindow' that matches the specified    binding constraints threw an exception.' Line number '5' and line position '9'

InnerException
{"Unable to cast object of type 'System.Windows.Controls.Button' to type 'System.Windows.Controls.MenuItem'."}

Any Guidance would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2025-01-04 20:53:23

您试图在这里将 Button 转换为 MenuItem

MenuItem menuItem = (MenuItem)e.OriginalSource;
Button button = (Button)e.OriginalSource;

我忘记了它的确切术语,但以这种方式转换它:

MenuItem menuItem = e.OriginalSource as MenuItem;
Button button = e.OriginalSource as Button;

此方法将返回 null 如果被转换的对象不是预期的类型,并且不会抛出异常。在尝试使用 menuItembutton 变量之前,请确保测试它们不为 null

You're trying to cast a Button as a MenuItem here:

MenuItem menuItem = (MenuItem)e.OriginalSource;
Button button = (Button)e.OriginalSource;

I forget the exact term for it, but cast it this way instead:

MenuItem menuItem = e.OriginalSource as MenuItem;
Button button = e.OriginalSource as Button;

This method will return null if the object being cast is not of the expected type, and will not throw an exception. Just be sure you test that your menuItem and button variables are not null before trying to use them.

你另情深 2025-01-04 20:53:23

而不是像这样检查源类型...

if (e.OriginalSource == menuItem)

...您可以像这样检查它:

if(e.OriginalSource is MenuItem)

然后您可以将变量声明移动到 if 块内。所以你的最终代码如下所示:

public void btnGeneral_Click(object sender, RoutedEventArgs e)
{   
    PanelMainContent.Children.Clear();

    if (e.OriginalSource is MenuItem)
    {
        MenuItem menuItem = (MenuItem)e.OriginalSource;
        PanelMainWrapper.Header = menuItem.Header;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        PanelMainContent.Children.Add(_userControls[menuItem.Tag.ToString()]);
    }

    if (e.OriginalSource is Button)
    {
        Button button = (Button)e.OriginalSource;
        PanelMainWrapper.Header = button.Content;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
    }
}

Instead checking the source type like this...

if (e.OriginalSource == menuItem)

...you can instead check it like this:

if(e.OriginalSource is MenuItem)

Then you can move your variable declarations inside your if blocks. So your final code looks like this:

public void btnGeneral_Click(object sender, RoutedEventArgs e)
{   
    PanelMainContent.Children.Clear();

    if (e.OriginalSource is MenuItem)
    {
        MenuItem menuItem = (MenuItem)e.OriginalSource;
        PanelMainWrapper.Header = menuItem.Header;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        PanelMainContent.Children.Add(_userControls[menuItem.Tag.ToString()]);
    }

    if (e.OriginalSource is Button)
    {
        Button button = (Button)e.OriginalSource;
        PanelMainWrapper.Header = button.Content;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文