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

HierarchicalDataTemplate 同一种数据类型嵌套   Menu  MenuItem  事件       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);
        }

  

上一篇:积分级数题集04


下一篇:git-使用