一、XML文件

<?xml version="1.0" encoding="utf-8" ?>
<MenuData xmlns="">
<Operation Name="文件" Gesture="F">
<Operation Name="新建" Gesture="N">
<Operation Name="网站" Gesture="Control+W">
</Operation>
<Operation Name="文档" Gesture="Control+D">
</Operation>
<Operation Name="项目" Gesture="Control+P">
</Operation>
</Operation>
</Operation>
<Operation Name="编辑" Gesture="E">
<Operation Name="复制" Gesture="Control+C">
</Operation>
<Operation Name="粘贴" Gesture="Control+V">
</Operation>
</Operation>
<Operation Name="帮助" Gesture="E">
</Operation>
</MenuData>

二、Xaml文件

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="menudata" Source="MenuData.xml" XPath="MenuData/Operation"></XmlDataProvider>
<HierarchicalDataTemplate DataType="Operation"
ItemsSource="{Binding XPath=Operation}">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding XPath=@Name}" HorizontalAlignment="Left"></TextBlock>
<TextBlock Text="{Binding XPath=@Gesture}" HorizontalAlignment="Left"></TextBlock>
</StackPanel> </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<StackPanel MenuItem.Click="StackPanel_Click"> <Menu ItemsSource="{Binding Source={StaticResource menudata}}"></Menu> </StackPanel> </Grid>
</Window>

三、后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml; namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void StackPanel_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = e.OriginalSource as MenuItem;
XmlElement xe = mi.Header as XmlElement;
MessageBox.Show(xe.Attributes["Name"].Value);
}
}
}

WPF之Menu绑定XML的更多相关文章

  1. WPF数据双向绑定

    设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人. 即:Pr ...

  2. res\menu\main.xml:6: error: No resource identifier found for attribute 'showAsAction' in package 'com.xxx.xxxx'

    res\menu\main.xml:6: error: No resource identifier found for attribute 'showAsAction' in package 'co ...

  3. WPF的DataTrigger绑定自身属性

    原文:WPF的DataTrigger绑定自身属性 <DataTrigger Binding="{Binding RelativeSource={RelativeSource self} ...

  4. (WPF) ComboBox 之绑定

    1.  在UI(Xaml) 里面直接绑定数据. <Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxSample& ...

  5. WPF 支持集合绑定的控件

    WPF 支持集合绑定的控件 ListBox ComboBox ListView DataGrid

  6. WPF UserControl 的绑定事件、属性、附加属性

    原文:WPF UserControl 的绑定事件.属性.附加属性 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/arti ...

  7. 封装:WPF中可以绑定的BindPassWord控件

    原文:封装:WPF中可以绑定的BindPassWord控件 一.目的:本身自带的PassWord不支持绑定 二.Xaml部分 <UserControl x:Class="HeBianG ...

  8. wpf直接绑定xml生成应用程序

    目的:在vs2010下用wpf完成一个配置工具,配置文件为xml格式,xml文件作为数据源,直接和wpf前台绑定,生成exe后,运行exe能够加载同路径下的xml配置文件并显示 xml文件在项目中的设 ...

  9. WPF 关于ComboBox在前台绑定XML数据的一些方法,使用XML数据提供器 XmlDataProvider

    关于使用 数据提供器:XmlDataProvider 的一些问题,以及在WPF中是如何使用的一些介绍,还有踩到的一些坑,希望其他和我碰到一样问题的,可以更快的解决. 首先,要求是 在WPF 的前台代码 ...

随机推荐

  1. JS中JSON.parse和eval的异同

    1.相同点 JSON.parse和eval函数都可将一段json字符串转换为json对象,如: var json = '{"intro":[{"name":&q ...

  2. c#代码混淆

    1.C#编写的dll库如何加密 2..NET Reactor使用教程 3..NET Reactor中各种混淆含义.原理 4..NET Reactor制作软件许可证 5..NET Reactor混淆代码 ...

  3. How to change the implementation (detour) of an externally declared function

    原文地址:http://stackoverflow.com/questions/6905287/how-to-change-the-implementation-detour-of-an-extern ...

  4. Spring Boot中CrudRepository与JpaRepository Dao中JpaRepository和JpaSpecificationExecutor查询

    原文地址  https://blog.csdn.net/xuemengrui12/article/details/80525227?utm_source=blogxgwz0 https://www.i ...

  5. Visual Studio 2015 与 .NET 4.6 RTM 正式发布

    原文地址 微软终于正式发布了Visual Studio 2015产品家族的RTM版本,此次发布体现了微软在开发工具发展方向上的转变迈出了重要的一步,他们致力于提供一种支持在所有主流应用平台上进行应用开 ...

  6. Mocks Aren't Stubs

    Mocks Aren't Stubs The term 'Mock Objects' has become a popular one to describe special case objects ...

  7. IDEA环境设置

    设置SDK:https://blog.csdn.net/y999666/article/details/51893348 打开模板使用说明,找到Maven本地安装目录, 备份E:\Program Fi ...

  8. 【iCore4 双核心板_FPGA】例程十一:FSMC总线通信实验——独立地址模式

    实验原理: STM32F767上自带FMC控制器,本实验将通过FMC总线的地址独立模式实现STM32与FPGA 之间通信,FPGA内部建立RAM块,FPGA桥接STM32和RAM块,本实验通过FSMC ...

  9. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-11层驱动之FSMC

    视频简介:该视频介绍iCore3应用开发平台中FSMC通信的配置方法及ARM与FPGA通信的方法. 源视频包下载地址:链接:http://pan.baidu.com/s/1slbHOCH 密码:n06 ...

  10. Git 藏匿操作

    假设您正在为您的产品实施的一项新功能.你的代码是在推进开发进度而客户不断升级需求突然来了.正因为如此,你必须保持放下你的新功能,工作几个小时.你不能提交你的部分代码,也不能扔掉你的变化.所以,你需要一 ...