菜单项和菜单项的问题按钮事件
我正在尝试将用户控件作为页面传递。我让它与按钮一起工作。当我添加带有单击事件的菜单时,它不再起作用。
这是代码块,它确定将哪个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您试图在这里将
Button
转换为MenuItem
:我忘记了它的确切术语,但以这种方式转换它:
此方法将返回
null
如果被转换的对象不是预期的类型,并且不会抛出异常。在尝试使用menuItem
和button
变量之前,请确保测试它们不为null
。You're trying to cast a
Button
as aMenuItem
here:I forget the exact term for it, but cast it this way instead:
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 yourmenuItem
andbutton
variables are notnull
before trying to use them.而不是像这样检查源类型...
...您可以像这样检查它:
然后您可以将变量声明移动到
if
块内。所以你的最终代码如下所示:Instead checking the source type like this...
...you can instead check it like this:
Then you can move your variable declarations inside your
if
blocks. So your final code looks like this: