近一个月,学习了WPF相关知识。在这里,我利用构建一个类似Office 2007框架的WPF应用程序,来总结学习WPF的个人体验。
一、需求
利用WPF,构建一个程序,框架类似Office 2007.如图:
二、关于Ribbon
这里就不对Ribbon功能区做详细介绍,网上可以搜索相关资料。
只想说明,在VS2012及以前的版本,没有内置Ribbon控件。需要到微软的Download Center搜索“WPF Ribbon”,下载Microsoft Ribbon for WPF.msi文件来安装。
VS2015已经内置Ribbon控件,直接使用就好。
三、新建项目
我们使用微软提供的WPF应用程序模板,来创建新项目。
启动VS2015,执行File ->New -> Project,会弹出下列新项目窗口,接着,我们选择Templates -> Visual C# -> Windows -> WPF Application,最后修改项目名和存放目录(我这里就用默认),点击OK按钮,即生成一个WPF应用程序模板。
四、UI设计
我们先看MainWindow.xaml的最初代码,简单得很,就两个标签Window和Grid,如下:
<Window xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" x:Class="WpfApplication1.MainWindow"
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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
回顾我们的需求:一个Ribbon功能区,一个主窗口和一个状态栏。那UI布局就简单将Grid分三行一列就可以了。分别是:
1) 第一行放Ribbon控件。
2) 第二行放Frame控件。Frame控件作为嵌套Page和UserControl等窗体的顶级控件。
3) 第三行放StatusBar控件。
<Window xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" x:Class="WpfApplication1.MainWindow"
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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<my:Ribbon Grid.Row="0">
</my:Ribbon>
<Frame Grid.Row="1">
</Frame>
<StatusBar Grid.Row="2">
</StatusBar>
</Grid>
</Window>
编译,运行,最简单的类似Office 2007的程序如下:
先写到这里,下一篇文章介绍Ribbon功能区和状态栏的处理。
-------------------
源码下载地址:http://download.csdn.net/detail/mandylover/9737118