WPF learning 10: Drawing editing tool based on MVVM Light (1), wpfmvvm

Source: Internet
Author: User

WPF learning 10: Drawing editing tool based on MVVM Light (1), wpfmvvm

Shows the functions of the graphic editor:

In addition to the MVVM Light framework, this article describes the basics of WPF 0-9 learning.

In this section, we will set up the Editor interface and build an MVVM Light framework environment.

 

Interface

<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. ColumnDefinitions> <ColumnDefinition Width = "auto"/> <ColumnDefinition Width = "*"/> </Grid. columnDefinitions> <Grid. rowDefinitions> <RowDefinition Height = "auto"/> <RowDefinition Height = "*"/> </Grid. rowDefinitions> <! -- Toolbar --> <StackPanel Grid. row = "0" Orientation = "Horizontal" Margin = "5 5 5" Grid. columnSpan = "2"> <Button Margin = "10 0 10 0"> Configure </Button> <TextBlock verticalignment = "Center"> box color: </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"> fill color: </TextBlock> <ComboBox Width = "100" Margin = "0 0 10 0"> </ComboBox> <Button Margin = "0 0 10 0"> output </Button> </StackPanel> <! -- Select status bar --> <ToolBarTray Grid. column = "0" Grid. rowSpan = "2" Margin = "0 50 0 0" Orientation = "Vertical"> <ToolBar> <RadioButton Style = "{StaticResource StatusBarButton}"> scaling </RadioButton> <RadioButton style = "{StaticResource StatusBarButton}"> move </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> </Border> </Canvas> </ScrollViewer> </Grid> </Window>

MVVM Light

First, add a reference file.

The first three are framework components, and ServiceLocation is responsible for Dependency inversion. With Interactivity, we can extend the XAML so that we can use the foreground code to complete the function of passing parameters to ViewModel.

Create the ViewModel folder and create MainViewModel ViewModelLocator.

MainViewModel inherits ViewModelBase. Here we will write a Command example.

Class MainViewModel: ViewModelBase {private ICommand _ showPrompt; public ICommand ShowPrompt {get {// The previous Lambda is Excute, and the last is CanExcute return _ showPrompt ?? (_ ShowPrompt = new RelayCommand () => MessageBox. Show ("Hello World"), () => true ));}}}

 

Register ViewModel in Locator

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()    {    }}

Create container resources in App. xaml.

The circled part is the code to be added.

Configure DataContext in the front-end code of the main form and add the reference of Interactivity by the way.

<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}}">

Modify the code to test whether the framework is successfully used.

<Button Margin = "0 0 10 0" Command = "{Binding ShowPrompt}"> output </Button>

Result:

The next section generates the canvas and draws the three basic shapes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.