图形编辑器的功能如下图所示:

除了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)的更多相关文章

  1. WPF学习12:基于MVVM Light 制作图形编辑工具(3)

    本文是WPF学习11:基于MVVM Light 制作图形编辑工具(2)的后续 这一次的目标是完成 两个任务. 本节完成后的效果: 本文分为三个部分: 1.对之前代码不合理的地方重新设计. 2.图形可选 ...

  2. WPF学习11:基于MVVM Light 制作图形编辑工具(2)

    本文是WPF学习10:基于MVVM Light 制作图形编辑工具(1)的后续 这一次的目标是完成 两个任务. 画布 效果: 画布上,选择的方案是:直接以Image作为画布,使用RenderTarget ...

  3. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML

    WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...

  4. WPF学习08:MVVM 预备知识之COMMAND

    WPF内建的COMMAND是GOF 提出的23种设计模式中,命令模式的实现. 本文是WPF学习07:MVVM 预备知识之数据绑定的后续,将说明实现COMMAND的三个重点:ICommand  Comm ...

  5. 【Telerik控件学习】-建立自己的图形编辑工具(Diagram)

    Telerik提供了RadDiagram控件,用于图形元素的旋转,拖拽和缩放.更重要的是,它还拓展了许多绑定的命令(复制,剪切,粘贴,回退等等). 我们可以用来组织自己的图形编辑工具. Step1.定 ...

  6. MAPZONE GIS SDK接入Openlayers3之五——图形编辑工具

    图形编辑工具提供对要素图形进行增.删.改的功能,具体包括以下几种工具类型: 浏览工具 选择工具 创建要素工具 删除命令 分割工具 合并命令 节点编辑工具 修边工具 撤销命令 重做命令 工具的实现基本上 ...

  7. WPF学习07:MVVM 预备知识之数据绑定

    MVVM是一种模式,而WPF的数据绑定机制是一种WPF内建的功能集,两者是不相关的. 但是,借助WPF各种内建功能集,如数据绑定.命令.数据模板,我们可以高效的在WPF上实现MVVM.因此,我们需要对 ...

  8. WPF学习笔记-用Expression Blend制作自定义按钮

    1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点击此按钮,选择Edit control parts(template)>Edit a cop ...

  9. WPF学习笔记:MVVM模式下,ViewModel如何关闭View?

    原文:http://blog.csdn.net/leftfist/article/details/32349731 矫枉过正,从一个极端走向另一个极端.MVVM模式,View只负责呈现,虽然也有后台代 ...

随机推荐

  1. 【Mongodb教程 第一课 】 MongoDB下载安装

    MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.以window平台 ...

  2. Qt 用户登录界面

     使用QT创建自己的登录窗口: 主要步骤:    1.窗口界面的绘制     2.沟通数据库进行密码验证 void MainWindow::on_pushButton_clicked() { // 连 ...

  3. Linux IO 多路复用是什么意思?

    写在前面:本文整理于知乎,原文链接为http://www.zhihu.com/question/32163005/answer/55772739,作者:罗志宇 再次向作者表示感谢~~ 假设你是一个机场 ...

  4. ExtJs 中获取 radiobutton 的值

    ExtJs中使用radiobutton,想在ExtJs本身代码中(注意,不是在后台喔)获取 radioButton 的值,居然发现很难,无从下手的感觉.后来在网上寻寻觅觅,痛苦摸索之后,真是大跌眼镜. ...

  5. while语句字符串的基本操作

    1,编码:对现在通用文字编码成计算机文字,便于储存,传递,交流. 最早的计算机编码是ACSII美国人创建的,包含英文字母,数字,以及特殊符号.总共是128个码位:2**7,因为计算机的底层只能识别:& ...

  6. string string.h cstring 区别

    c++中 string与string.h 的作用和区别 #include  <string.h>  void  main()  {        string  aaa=  "a ...

  7. CRM 插件奇怪的报错

    CRM插件,数据库方式注册.报错 找不到方法:“Void Microsoft.Xrm.Sdk.Entity..ctor(System.String, System.Guid)”. 这个错误让人摸不着头 ...

  8. HDU1054 Strategic Game —— 最小点覆盖 or 树形DP

    题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others)     ...

  9. YTU 2875: 倒霉蛋买饭去

    2875: 倒霉蛋买饭去 时间限制: 1 Sec  内存限制: 128 MB 提交: 22  解决: 17 题目描述 早春星期天的某个早晨,大风呼呼地刮.一个宿舍n个人,谁也不想起床买饭去.他们定了一 ...

  10. POJ - 1470 Closest Common Ancestors(离线Tarjan算法)

    1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...