2008-12-01 37 views
9

Tôi đã có một MenuItem whos ItemsSource là databound đến một danh sách đơn giản của chuỗi, nó hiển thị chính xác, nhưng tôi đang đấu tranh để xem làm thế nào tôi có thể xử lý các sự kiện bấm cho họ!Làm cách nào để xử lý các sự kiện bấm trong menu dữ liệu bị ràng buộc trong WPF

Dưới đây là một ứng dụng đơn giản mà thể hiện nó:

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

Rất cám ơn!

Chris.

Trả lời

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

Mã đằng sau ..

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

Sự kiện sẽ bong bóng lên đến xử lý chung. Sau đó bạn có thể sử dụng văn bản Tiêu đề hoặc tốt hơn tài sản DataContext để chuyển đổi và hành động khi cần thiết

4

Bạn có thể có từng mục menu thực thi cùng một lệnh, do đó xử lý việc thực thi tập trung. Nếu bạn cần phải phân biệt các mục menu ngoài trường hợp đối tượng thực tế, bạn có thể ràng buộc các tham số lệnh quá:

<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 được giả định là một tài sản trên từng hạng mục trong bộ sưu tập MyMenuItems của bạn. Trình xử lý thực thi lệnh của bạn do đó sẽ nhận được giá trị SomeProperty cho mục menu cụ thể được nhấp.

+0

này vẫn sẽ dẫn đến một handler trung ương, mà chuyển vào giá trị SomeProperty. phải không? – Gishu

+0

Vâng, đúng vậy. Trên thực tế, thông số không bắt buộc. Bạn có thể phân nhánh dựa trên cá thể MenuItem thay vào đó, nhưng tôi đã tìm thấy thường có một đoạn dữ liệu cụ thể cho từng mục menu được yêu cầu. ví dụ. xem xét các mục trình đơn tệp gần đây. Phần thông tin quan trọng là đường dẫn tệp. –

+0

Khi tôi kiểm tra lệnh này sẽ kích hoạt trên cả hai nút chuột trái và phải. Làm cách nào để xác định nút nào được nhấn trong lệnh? – keft

1

xử lý sự kiện IMHO tổng quát hơn với khả năng để có được mục từ 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 
} 

Hope it'l giúp smbd!

0

Nếu bạn muốn có một cách đơn giản hơn để truy cập vào nội dung mục menu:

<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> 

Cod Đằng sau:

private void MenuItem_Click(object sender, RoutedEventArgs e) 
{ 
    var item = ((MenuItem)e.OriginalSource).CommandParameter; 
} 
Các vấn đề liên quan