none
MenuItem如何获取被点击的MenuItem绑定的实体对象? RRS feed

  • 问题

  • 将一个MenuItem绑定一个泛型集合,然后设置该menuitem下的所有子menuitem的Click事件样式。试图触发点击事件通过sender对象获取,但是没效果。如何获取被点击的菜单绑定的实体对象?

    MenuClick是一个MenuItem的样式,里面设置了该menuitem下的每一个子Menuitem的点击事件。

     <MenuItem Header="历史记录" Visibility="Collapsed" ItemsSource="{Binding list}" ItemContainerStyle="{StaticResource MenuClick}"  FontSize="18"
                                         VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.CanContentScroll="True" >
                                    
                                    <MenuItem.ItemTemplate>
                                        <DataTemplate >
                                            <StackPanel>
                                                <TextBlock   TextTrimming="CharacterEllipsis" FontSize="15"   Text="{Binding Name}"    ></TextBlock>
                                            </StackPanel>
                                        </DataTemplate>
                                       
                                    </MenuItem.ItemTemplate>
                                    <MenuItem.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <VirtualizingStackPanel>
    
                                            </VirtualizingStackPanel>
                                        </ItemsPanelTemplate>
                                    </MenuItem.ItemsPanel>
                                </MenuItem>

    如何在点击Menuitem时获取TextBlock绑定的值?



    2016年12月30日 3:33

答案


  • Hi 轮回的齿轮,

    你可以参考下面的代码去绑定你的MenuItem,并通过点击事件获取绑定的源。

    XAML:

    <Grid Background="Red" Height="100" Width="100" x:Name="gridds">
            <Grid.Resources>
                <ContextMenu x:Name="cmuen" x:Key="TextBlockContextMenu" >
                    <MenuItem 
                    Header="Change Chooes" 
                    CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}"  ItemsSource="{Binding}">
                        <MenuItem.Resources>
                            <Style TargetType="{x:Type MenuItem}">
                                <EventSetter Event="Click" Handler="EditStatusCm_Click"/>
                            </Style>
                        </MenuItem.Resources>
                        <MenuItem.ItemTemplate>
                            <DataTemplate >
                                <StackPanel>
                                    <TextBlock   TextTrimming="CharacterEllipsis" FontSize="15"   Text="{Binding Path=Name}"    ></TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </MenuItem.ItemTemplate>
                    </MenuItem>
    
                    <MenuItem 
                    Header="MenuItem Item2" 
                    Click="EditStatusCm_Click"
                    CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}" />
                </ContextMenu>
    
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="ContextMenu" Value="{StaticResource TextBlockContextMenu}" />
                </Style>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Text="Test  one" Grid.Row="0" />
            <TextBlock Text="Test  two" Grid.Row="1" />
        </Grid>
    

    XAML.CS:

     public partial class GetMenuItemdemo : Window
        {
            public GetMenuItemdemo()
            {
                List<a2> aas = new List<a2>();
                aas.Add(new a2() { Name = "TEST1" });
                aas.Add(new a2() { Name = "TEST2" });
                InitializeComponent();
                gridds.DataContext = aas;
            }
    
            string showmessage = "";
            int  count = 1;
            private void EditStatusCm_Click(object sender, RoutedEventArgs e)
            {
                MenuItem mi = sender as MenuItem;
                if (mi != null)
                {
                    ItemCollection icll  = mi.Items;
                    Object sourdes = mi.DataContext;
                    ContextMenu cm = mi.CommandParameter as ContextMenu;
                    if (cm != null)
                    {
                        TextBlock g = cm.PlacementTarget as TextBlock;
                        if (g != null)
                        {
                            if (count == 2)
                            {
                                showmessage += "父节点: " + mi.Header;
                                MessageBox.Show(showmessage);
                                showmessage = "";
                                count = 1;
                            }
                        }
                    }else
                    {
                        if (count == 1)
                        {
                            a2 getobjec = (a2)mi.Header;
                            showmessage += "子节点: " + getobjec.Name + "; "; ;
                            count++;
                        }
                    }
                }
            }
        }
    
        public class a2
        {
            public string Name { get; set; }
        }

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年12月30日 7:28
    版主