HierarchicalDataTemplate 同一种数据类型嵌套 Menu MenuItem 事件

2021/8/28 6:06:08

本文主要是介绍HierarchicalDataTemplate 同一种数据类型嵌套 Menu MenuItem 事件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

       

 

 

 注意   HierarchicalDataTemplate 作用的目标是 MenuItem的 Header

<Window x:Class="WpfDemo.HierarchicalDataTemplateDemo1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfDemo"
        mc:Ignorable="d"
        Title="HierarchicalDataTemplateDemo1" Height="300" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="ds" XPath="Data/Operation">
            <x:XData>
                <Data xmlns="">
                    <Operation Name="文件" Gesture="F">
                        <Operation Name="新建" Gesture="N">
                            <Operation Name="项目" Gesture="Control+P"></Operation>
                            <Operation Name="网址" Gesture="Control+W"></Operation>
                            <Operation Name="文档" Gesture="Control+D"></Operation>
                        </Operation>
                        <Operation Name="保存" Gesture="S"></Operation>
                        <Operation Name="打印" Gesture="P"></Operation>
                        <Operation Name="退出" Gesture="X"></Operation>
                    </Operation>
                    <Operation Name="编辑" Gesture="E">
                        <Operation Name="拷贝" Gesture="Control+C"></Operation>
                        <Operation Name="剪切" Gesture="Control+X"></Operation>
                        <Operation Name="粘贴" Gesture="Control+V"></Operation>
                    </Operation>
                </Data>
            </x:XData>
        </XmlDataProvider>
        <!--MenuItem模板-->
        <HierarchicalDataTemplate DataType="Operation" ItemsSource="{Binding XPath=Operation}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding XPath=@Name}"></TextBlock>
                <TextBlock Text="{Binding XPath=@Gesture}"></TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        
    </Window.Resources>
    <Grid MenuItem.Click="Grid_Click">
        <Menu ItemsSource="{Binding Source={StaticResource ds}}"></Menu>
    </Grid>
</Window>

  

private void Grid_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = e.OriginalSource as MenuItem;
            XmlElement xe = item.Header as XmlElement;
            MessageBox.Show(xe.Attributes["Name"].Value+ xe.Attributes["Gesture"].Value);
        }

  



这篇关于HierarchicalDataTemplate 同一种数据类型嵌套 Menu MenuItem 事件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程