2008-12-01 94 views
9

我有一個MenuItem誰的ItemsSource是數據綁定到一個簡單的字符串列表,它顯示正確,但我很努力地看到我如何處理點擊事件爲他們!我如何處理WPF中的數據綁定菜單中的點擊事件

下面是一個簡單的應用程序,演示了:

<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?!"); 
     } 
    } 
} 

非常感謝!

Chris。

回答

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

後面的代碼..

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

活動將泡沫到通用處理器。然後,您可以使用標題文字或更好的DataContext屬性切換,並作爲需要

4

您可以讓每個菜單項執行相同的命令,從而集中處理執行。如果您需要區分菜單項超出了實際對象實例,你可以在命令參數綁定過:

<MenuItem Header="My Items" ItemsSource="{Binding Path=MyMenuItems}"> 
    <MenuItem.ItemContainerStyle> 
     <Style TargetType="MenuItem"> 
      <Setter Property="Command" Value="{x:Static local:MyCommands.MyCommand}"/> 
      <Setter Property="CommandParameter" Value="{Binding SomeProperty}"/> 
     </Style> 
    </MenuItem.ItemContainerStyle> 
</MenuItem> 

SomeProperty被認爲是對每個項目的屬性在您的收藏MyMenuItems。因此,您的命令執行處理程序將爲被點擊的特定菜單項獲得值SomeProperty

+0

這仍然導致中央處理程序,它接通了SomeProperty值。不是嗎? – Gishu 2008-12-01 19:01:22

1

恕我直言,更一般的事件處理程序的能力從的ItemsSource

private void DataBoundMenuItem_Click(object sender, RoutedEventArgs e) 
{ 
    // get menu item with ItemsSource bound 
    var myItemsMenuItems = sender as MenuItem; 

    // get submenu clicked item constructed from MyMenuItems collection 
    var myItemsMenuSubItem = e.OriginalSource as MenuItem; 

    // get underlying MyMenuItems collection item 
    var o = myItemsMenuItems 
     .ItemContainerGenerator 
     .ItemFromContainer(myItemsMenuSubItem); 
    // convert to MyMenuItems type ... in our case string 
    var itemObj = o as (string); 

    // TODO some processing 
} 

獲得項目希望it'l幫助SMBD!

0

如果你想有一個更簡單的進入菜單項內容的方式:

<MenuItem Header="My Items" ItemsSource="{Binding Path=MyMenuItems}" Click="MenuItem_Click"> 
    <MenuItem.ItemContainerStyle> 
     <Style TargetType="MenuItem"> 
      <Setter Property="CommandParameter" Value="{Binding}" /> 
     </Style> 
    </MenuItem.ItemContainerStyle> 
</MenuItem> 

鱈魚的背後:

private void MenuItem_Click(object sender, RoutedEventArgs e) 
{ 
    var item = ((MenuItem)e.OriginalSource).CommandParameter; 
} 
相關問題