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>

结果:

下一节将会完成画布的生成与三个基本形状的绘制。

时间: 2024-08-28 18:48:44

WPF学习10:基于MVVM Light 制作图形编辑工具(1)的相关文章

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

本文是WPF学习10:基于MVVM Light 制作图形编辑工具(1)的后续 这一次的目标是完成 两个任务. 画布 效果: 画布上,选择的方案是:直接以Image作为画布,使用RenderTargetBitmap绑定为Image的图片源,这样可以为后续的导出图片功能提供很大的便利. 对拖动栏XAML进行如下修改: <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="

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

WPF内建的COMMAND是GOF 提出的23种设计模式中,命令模式的实现. 本文是WPF学习07:MVVM 预备知识之数据绑定的后续,将说明实现COMMAND的三个重点:ICommand  CommandManager InputBindings COMMAND简介 一般情况我们应用设计如下,一个个控件的各类Handler直接关心了如何实现具体的应用逻辑. 借助COMMAND,我们将具体实现的应用逻辑放在COMMAND中实现,控件只需要绑定相应的COMMAND,而无需关心应用逻辑,从而实现界面

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

Telerik提供了RadDiagram控件,用于图形元素的旋转,拖拽和缩放.更重要的是,它还拓展了许多绑定的命令(复制,剪切,粘贴,回退等等). 我们可以用来组织自己的图形编辑工具. Step1.定义图形元素容器(Shape)的基类,继承RadDiagramShape,并重写Serialize和Deserialize方法,来定制将来对象的保存或复制. /// <summary> /// 图形Shape控件 /// </summary> public class FigureSha

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

MVVM是一种模式,而WPF的数据绑定机制是一种WPF内建的功能集,两者是不相关的. 但是,借助WPF各种内建功能集,如数据绑定.命令.数据模板,我们可以高效的在WPF上实现MVVM.因此,我们需要对各种MVVM相关的WPF内建功能集进行了解,才能在扎实的基础上对MVVM进行学习与实践. 本文是WPF学习03:Element Binding的后续,将说明实现数据绑定的三个重点:DataContext INotifyPropertyChanged IValueConverter MVVM简介 MV

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

1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点击此按钮,选择Edit control parts(template)>Edit a copy... 3.在弹出的Create style resource对话框中,修改新按钮样式的名称 4.在左侧的Object and timeline面板中选中ContentPresenter元素,按Ctrl+X将此标记临时保存到内存中 5.选中Chrome,按Delete键删除 6.选中Template,在

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

原文:http://blog.csdn.net/leftfist/article/details/32349731 矫枉过正,从一个极端走向另一个极端.MVVM模式,View只负责呈现,虽然也有后台代码,但基本上就是摆设,VM接管了一切的逻辑处理. 那么,现在,大能的VM已经完成了所有的事情,这个窗口V如何才能自动关闭呢? 据我目前少得可怜的WPF知识可知,有两种方案: 方案一.利用View里的IsEnable属性. 原理是这样的: 1.UI中的IsEnabled绑定VM中的属性 2.UI的后台

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

第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大,尤其是象我这样毫无美术基础,毫无艺术细胞的人而言.因此可以找个参照物,比如一张数码照片,对着这个图片描,可以大体上把物体的轮廓描出来. 2.Ctrl+C然后Ctrl+V,在Expression Design中添加一张位图,注意,此时Design会自动新建一个图层(layer),双击修改图层名为MousePhoto: 3.单击Layer面板右下角的New layer按钮,添加新图层

MVVM Light须要注意的10个问题

MVVM Light须要注意的10个问题 从使用XAML技术基础開始(实际上并非非常久曾经).我便关注MVVM(Model – View – ViewModel)模式.偶然接触到MVVM Light不久后便喜欢上它的工作方式. 不光我包含业余和专业开发者在内的非常多开发者都喜欢这个函数库. 依照开发者意愿,MVVM Light 不是一个框架而是函数库,该函数库注重于探究建立一个MVVM结构而且提供一些额外的帮助类以便于应用. MVVM Light在发展过程中改变了非常多.非常多元素被增加又有非常

MVVM Light需要注意的10个问题

MVVM Light需要注意的10个问题 从使用XAML技术基础开始(实际上并不是很久以前),我便关注MVVM(Model – View – ViewModel)模式.偶然接触到MVVM Light不久后便喜欢上它的工作方式.不光我包括业余和专业开发人员在内的很多开发人员都喜欢这个函数库.按照开发者意愿,MVVM Light 不是一个框架而是函数库,该函数库注重于探究建立一个MVVM结构并且提供一些额外的帮助类以便于应用. MVVM Light在发展过程中改变了很多,很多元素被加入又有很多元素被