WPF学习10:基于MVVM Light 制作图形编辑工具(1)
图形编辑器的功能如下图所示:

除了MVVM Light 框架是一个新东西之外,本文所涉及内容之前的WPF学习0-9基本都有相关介绍。
本节中,将搭建编辑器的界面,搭建MVVM Light 框架的使用环境。
界面

<Window x:Class="GraphEditor.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" Background="#FFFFFF">
<Window.Resources>
<Style x:Key="StatusBarButton" TargetType="RadioButton">
<Setter Property="Width" Value="30"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Margin" Value="0 10 0 0"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--工具栏-->
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5 5 5 5" Grid.ColumnSpan="2">
<Button Margin="10 0 10 0">配置</Button>
<TextBlock VerticalAlignment="Center">外框颜色:</TextBlock>
<ComboBox Width="100" Margin="0 0 10 0" ItemsSource="{Binding AvailiableColors}" SelectedItem="{Binding BorderColor}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Rectangle Width="100" Height="15" Fill="{Binding}"></Rectangle>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock VerticalAlignment="Center">填充颜色:</TextBlock>
<ComboBox Width="100" Margin="0 0 10 0"></ComboBox>
<Button Margin="0 0 10 0">输出</Button>
</StackPanel>
<!--状态选择栏-->
<ToolBarTray Grid.Column="0" Grid.RowSpan="2" Margin="0 50 0 0" Orientation="Vertical">
<ToolBar>
<RadioButton Style="{StaticResource StatusBarButton}">缩放</RadioButton>
<RadioButton Style="{StaticResource StatusBarButton}">移动</RadioButton>
<RadioButton Style="{StaticResource StatusBarButton}">
<Line X1="0" Y1="0" X2="15" Y2="15" Stroke="Black" StrokeThickness="1"></Line>
</RadioButton>
<RadioButton Style="{StaticResource StatusBarButton}">
<Rectangle Width="20" Height="15" Stroke="Black" StrokeThickness="1"></Rectangle>
</RadioButton>
<RadioButton Style="{StaticResource StatusBarButton}">
<Ellipse Width="20" Height="20" Stroke="Black" StrokeThickness="1"></Ellipse>
</RadioButton>
</ToolBar>
</ToolBarTray>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Column="1" Grid.Row="1">
<Canvas>
<Border>
<Image></Image>
</Border>
</Canvas>
</ScrollViewer>
</Grid>
</Window>
MVVM Light
首先是添加引用文件。

前三个为框架构成部分,ServiceLocation负责依赖反转。通过Interactivity我们可以扩展XAML,使得用前台代码就可以完成向ViewModel传参的功能。
创建ViewModel文件夹,其下创建MainViewModel ViewModelLocator两个文件。

MainViewModel继承ViewModelBase即可,我们在这里写一个Command的例子。
class MainViewModel : ViewModelBase
{
private ICommand _showPrompt;
public ICommand ShowPrompt
{
get
{
//前一个Lambda为Excute,后一个为CanExcute
return _showPrompt ?? (_showPrompt = new RelayCommand(() => MessageBox.Show("Hello World"), () => true));
}
}
}
在Locator中对ViewModel进行注册
class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
} public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
} public static void Cleanup()
{
}
}
在App.xaml中创建容器资源。

圈出的部分为需要添加的代码。
在主窗体的前台代码中配置DataContext,顺便添加Interactivity的引用。
<Window x:Class="GraphEditor.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" Background="#FFFFFF"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
DataContext="{Binding Main, Source={StaticResource ResourceKey=Locator}}">
我们修改一下代码,用于测试是否成功使用了框架。
<Button Margin="0 0 10 0" Command="{Binding ShowPrompt}">输出</Button>
结果:

下一节将会完成画布的生成与三个基本形状的绘制。
WPF学习10:基于MVVM Light 制作图形编辑工具(1)的更多相关文章
- WPF学习12:基于MVVM Light 制作图形编辑工具(3)
本文是WPF学习11:基于MVVM Light 制作图形编辑工具(2)的后续 这一次的目标是完成 两个任务. 本节完成后的效果: 本文分为三个部分: 1.对之前代码不合理的地方重新设计. 2.图形可选 ...
- WPF学习11:基于MVVM Light 制作图形编辑工具(2)
本文是WPF学习10:基于MVVM Light 制作图形编辑工具(1)的后续 这一次的目标是完成 两个任务. 画布 效果: 画布上,选择的方案是:直接以Image作为画布,使用RenderTarget ...
- WPF学习笔记-用Expression Design制作矢量图然后导出为XAML
WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...
- WPF学习08:MVVM 预备知识之COMMAND
WPF内建的COMMAND是GOF 提出的23种设计模式中,命令模式的实现. 本文是WPF学习07:MVVM 预备知识之数据绑定的后续,将说明实现COMMAND的三个重点:ICommand Comm ...
- 【Telerik控件学习】-建立自己的图形编辑工具(Diagram)
Telerik提供了RadDiagram控件,用于图形元素的旋转,拖拽和缩放.更重要的是,它还拓展了许多绑定的命令(复制,剪切,粘贴,回退等等). 我们可以用来组织自己的图形编辑工具. Step1.定 ...
- MAPZONE GIS SDK接入Openlayers3之五——图形编辑工具
图形编辑工具提供对要素图形进行增.删.改的功能,具体包括以下几种工具类型: 浏览工具 选择工具 创建要素工具 删除命令 分割工具 合并命令 节点编辑工具 修边工具 撤销命令 重做命令 工具的实现基本上 ...
- WPF学习07:MVVM 预备知识之数据绑定
MVVM是一种模式,而WPF的数据绑定机制是一种WPF内建的功能集,两者是不相关的. 但是,借助WPF各种内建功能集,如数据绑定.命令.数据模板,我们可以高效的在WPF上实现MVVM.因此,我们需要对 ...
- WPF学习笔记-用Expression Blend制作自定义按钮
1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点击此按钮,选择Edit control parts(template)>Edit a cop ...
- WPF学习笔记:MVVM模式下,ViewModel如何关闭View?
原文:http://blog.csdn.net/leftfist/article/details/32349731 矫枉过正,从一个极端走向另一个极端.MVVM模式,View只负责呈现,虽然也有后台代 ...
随机推荐
- PHP根据两点间的经纬度计算距离
/** * 说明: 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ function getDistance($lat ...
- MySQL 当记录不存在时插入,当记录存在时更新
第一种: 示例一:插入多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: INSERT INTO clients (client_id,client_name ...
- LoadRunner系列之—-04 录制基于https协议的脚本
实际性能测试过程中,有些需录制脚本的页面或接口是基于https协议的,按原来方法录制脚本,录完了脚本是空的.为解决这个问题,第一步了解https协议的具体实现,这块网上资料很多,可参考页面下方参考资料 ...
- 2012年公司组织旅游西安线个人记录(repost)
2012年公司组织旅游西安线个人记录 文件夹 [隐藏] 1 序言 2 第1天 3 第2天 4 第3天 5 第4天 6 第5天 [title=2012%E5%B9%B4%E5%85%AC%E5%8F% ...
- Selenium 工具介绍、安装、配置及使用
概念 selenium是一款跨平台开源工具,用于web的功能完整性測试,想做压力測试的能够换软件了 先看一眼官方站 http://docs.seleniumhq.org/download/ 首先要FQ ...
- Evaluate Reverse Polish Notation --leetcode
原题链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目大意:给出逆波兰式,然后求其结果. 解题方法:单个栈 ...
- jvm部分知识总结
1.jvm有三种执行模式,分别是解释执行,混合执行和编译执行,默认情况是混合执行模式. java version " Java(TM) SE Runtime Environment (bui ...
- java生成随机汉字
方法一: public static char getRandomChar() { return (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0 ...
- 一些linux嵌入式资源下载地址
linux内核源代码情景分析 非扫描版 上下册合订版 字清楚 带书签 1575页 pdfhttp://download.csdn.net/source/2002579***************** ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...