如何处理点击WPF中的数据绑定菜单事件 [英] How do I handle click events in a data bound menu in WPF

查看:774
本文介绍了如何处理点击WPF中的数据绑定菜单事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单项的ItemsSource卫生组织数据绑定到字符串,其显示正确的简单列表,但我挣扎,看我怎么可以处理单击事件他们!



下面是一个简单的应用程序,演示了:

 <窗口x:类=WPFDataBoundMenu.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300>
<网格和GT;
<菜单>
<菜单项标题=文件点击=MenuItem_Click/>
<菜单项标题=我的物品的ItemsSource ={绑定路径= MyMenuItems}/>
< /菜单>
< /网格和GT;





 使用System.Collections.Generic;使用System.Windows 
;

命名空间WPFDataBoundMenu
{
///<总结> $ B $为Window1.xaml
///< b ///交互逻辑; /总结>
公共部分类窗口1:窗口
{
公开名单<串GT; MyMenuItems {搞定;设置;}

公共窗口1()
{
的InitializeComponent();
MyMenuItems =新的List<串GT; {项目1,项目2,项目3};
的DataContext =这一点;
}

私人无效MenuItem_Click(对象发件人,RoutedEventArgs E)
{
MessageBox.Show(我怎么处理的其他点击?!);
}
}
}



非常感谢!



克里斯


解决方案

 <菜单项标题=我的物品的ItemsSource ={绑定路径= MyMenuItems}点击=DataBoundMenuItem_Click/> 



后面的代码..

 私人无效DataBoundMenuItem_Click(对象发件人,RoutedEventArgs E)
{
菜单项obMenuItem = e.OriginalSource的菜单项;
MessageBox.Show(的String.Format({0}只是说嗨!,obMenuItem.Header));
}



事件将泡沫到通用处理器。然后,可以使用的标题文字或更好的DataContext属性转换,并作为必要


I've got a MenuItem whos ItemsSource is databound to a simple list of strings, its showing correctly, but I'm struggling to see how I can handle click events for them!

Here's a simple app that demonstrates it:

<Window x:Class="WPFDataBoundMenu.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Menu>
        <MenuItem Header="File" Click="MenuItem_Click" />
        <MenuItem Header="My Items" ItemsSource="{Binding Path=MyMenuItems}" />
    </Menu>
</Grid>

using System.Collections.Generic;
using System.Windows;

namespace WPFDataBoundMenu
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public List<string> MyMenuItems { get; set;}

        public Window1()
        {
            InitializeComponent();
            MyMenuItems = new List<string> { "Item 1", "Item 2", "Item 3" };
            DataContext = this;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("how do i handle the other clicks?!");
        }
    }
}

Many thanks!

Chris.

解决方案

<MenuItem Header="My Items" ItemsSource="{Binding Path=MyMenuItems}" Click="DataBoundMenuItem_Click" />

Code behind..

private void DataBoundMenuItem_Click(object sender, RoutedEventArgs e)
{
   MenuItem obMenuItem = e.OriginalSource as MenuItem;
   MessageBox.Show( String.Format("{0} just said Hi!", obMenuItem.Header));
}

Events will bubble up to the common handler. You can then use the Header text or better the DataContext property to switch and act as needed

这篇关于如何处理点击WPF中的数据绑定菜单事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆