Livetを使ったC#のWPFプログラミング

システムとして機能が網羅されたサンプルをもとに、Livetを使ったC#のWPFシステムの開発技能を身に付ける為の学習サイト


12.明細画面


明細画面

前の一覧画面からこの明細画面へ遷移します。

View(画像をクリックすると拡大)

ZAMLは下記になります。


View
<Window x:Class="LivetWPFApplication100.Views.Window74"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
        xmlns:core="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=
        	Microsoft.Expression.Interactions"        
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
        xmlns:l="http://schemas.livet-mvvm.net/2011/wpf" 
        xmlns:v="clr-namespace:LivetWPFApplication100.Views"
        xmlns:vm="clr-namespace:LivetWPFApplication100.ViewModels"        
        xmlns:b="clr-namespace:LivetWPFApplication100.Behaviors"        
        Title="Window74" Height="714" Width="1035" 
        WindowStartupLocation="CenterScreen">
   
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Styles.xaml"/>
                <ResourceDictionary Source="/Resources/Styles2.xaml"/>
                <ResourceDictionary Source="/Resources/StylesBG.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <i:Interaction.Triggers>
        <!--WindowのContentRenderedイベントのタイミングでViewModelのInitialize
        	メソッドが呼ばれます-->
        <i:EventTrigger EventName="ContentRendered">
            <l:LivetCallMethodAction MethodTarget="{Binding}" MethodName=
            	"Initialize"/>
        </i:EventTrigger>

        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding Path=Loaded}" 
                CommandParameter=
                "{Binding Mode=OneTime,RelativeSource=
                {RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
        </i:EventTrigger>

        <i:EventTrigger EventName="Closing">
            <i:InvokeCommandAction Command="{Binding Path=Closing}"
                CommandParameter="{Binding Mode=OneTime,RelativeSource=
                {RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
        </i:EventTrigger>

        <l:InteractionMessageTrigger MessageKey="Close" Messenger=
        	"{Binding Messenger}">
            <l:WindowInteractionMessageAction/>
        </l:InteractionMessageTrigger>
    </i:Interaction.Triggers>

    <Grid Height="686" Width="1025"  Background="#FFE6E6FA" >

        <TextBox Background="AliceBlue" Height="23" HorizontalAlignment="Left" 
        	Margin="10,12,0,0" 
                 Name="txtMessage" 
                 Text="{Binding Path=txtMessage}" VerticalAlignment="Top" 
                 	Width="700" />
        <Button  Style="{StaticResource CommandTemplate}" Foreground="White"  
        	Content="" Height="40" HorizontalAlignment="Left" 
        	Margin="1009,613,0,0" Name="button15" VerticalAlignment="Top" 
        	Width="8" />
        <TextBox Style="{StaticResource HighlightTextBox}" 
                 Height="21" HorizontalAlignment="Right" Margin="0,52,717,0" 
                 Name="txt年度プロジェクトコード" 
                 Text="{Binding Path=txt年度プロジェクトコード}" VerticalAlignment=
                 "Top" Width="140" />
        <TextBox Style="{StaticResource HighlightTextBox}"  TextWrapping="Wrap" 
                 Height="37" HorizontalAlignment="Left" Margin="168,82,0,0" Name=
                 "txtプロジェクト名" 
                 Text="{Binding Path=txtプロジェクト名}" VerticalAlignment="Top" 
                 Width="140" />
        <TextBox Style="{StaticResource HighlightTextBox}"
                 Height="21" HorizontalAlignment="Right" Margin="0,134,717,0" 
                 Name="txt作成日" 
                 Text="{Binding Path=txt作成日}" VerticalAlignment="Top" 
                 Width="140" />
        <TextBox Style="{StaticResource HighlightTextBox}"
                 Height="21" HorizontalAlignment="Right" Margin="0,166,717,0" 
                 Name="txt部名" 
                 Text="{Binding Path=txt部名}" VerticalAlignment="Top" Width="140"/>
        <TextBox Style="{StaticResource HighlightTextBox}"
                 Height="21" HorizontalAlignment="Left" Margin="169,0,0,463" 
                 Name="txt作成者" 
                 Text="{Binding Path=txt作成者}" VerticalAlignment="Bottom" 
                 Width="139" />
        <TextBox Style="{StaticResource HighlightTextBox}"
                 Height="21" HorizontalAlignment="Right" Margin="0,234,717,0" 
                 Name="textBox1" 
                 Text="{Binding Path=txt販売金額,StringFormat={}{0:N2}}" 
                 VerticalAlignment="Top" Width="140" />
        <TextBox Style="{StaticResource HighlightTextBox}"
                 Height="21" HorizontalAlignment="Right" Margin="0,268,717,0" 
                 Name="textBox2" 
                 Text="{Binding Path=txt販売先名}" VerticalAlignment="Top" 
                 Width="140" />
        <TextBox Style="{StaticResource HighlightTextBox}"
                 Height="21" HorizontalAlignment="Right" Margin="0,302,717,0" 
                 Name="textBox3" 
                 Text="{Binding Path=txt検収}" VerticalAlignment="Top" Width="139"/>
        <TextBox Style="{StaticResource HighlightTextBox}"
                 Height="21" HorizontalAlignment="Left" Margin="169,0,0,336" 
                 Name="textBox4" 
                 Text="{Binding Path=txt仕入金額,StringFormat={}{0:N0}}" 
                 VerticalAlignment="Bottom" Width="139" />

        <Label Content="年度プロジェクトコード" Background="LightBlue" Height="21" 
        HorizontalAlignment="Left" Margin="35,52,0,0" Name="label1" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="プロジェクト名" Height="21" 
        HorizontalAlignment="Left" Margin="35,82,0,0" Name="label2" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="作成日" Height="21" 
        HorizontalAlignment="Left" Margin="35,132,0,0" Name="label3" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="部名" Height="21" 
        HorizontalAlignment="Left" Margin="35,166,0,0" Name="label4" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="作成者" Height="21" 
        HorizontalAlignment="Left" Margin="35,200,0,0" Name="label5" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="販売金額" Height="21" 
        HorizontalAlignment="Left" Margin="35,234,0,0" Name="label6" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="販売先名" Height="21" 
        HorizontalAlignment="Left" Margin="35,266,0,0" Name="label7" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="検収" Height="21" 
        HorizontalAlignment="Left" Margin="35,300,0,0" Name="label8" 
        VerticalAlignment="Top" Width="127" />
        <Label Background="LightBlue" Content="仕入金額" Height="21" 
        HorizontalAlignment="Left" Margin="35,329,0,0" Name="label9" 
        VerticalAlignment="Top" Width="127" />

        <v:UserControlFooterN2  Margin="0,0,0,0" HorizontalAlignment="Left" 
        VerticalAlignment="Bottom"  />

    </Grid>
</Window>


ViewModelは下記の通りです。
68行目のpublic ViewModel74(Project project)は、モデルのprojectを引数としています。
91行目のCopyExecute()で、このモデルのデータを122~137行でTextBoxに貼り付けています。


ViewModel
using System;

// ObservableCollection
using System.Collections.Generic;
using System.Linq;

//INotifyPropertyChanged
//PropertyChanged
using System.ComponentModel;

using Livet;
using Livet.Commands;
using Livet.Messaging;
using Livet.Messaging.IO;
using Livet.EventListeners;

//CloseCommand
using Livet.Messaging.Windows;

using System.Windows.Interactivity;

//ICommand
using System.Windows.Input;

//MessageBox
using System.Windows;

using Common;

using LivetWPFApplication100.ViewModels;
using LivetWPFApplication100.Models;

namespace LivetWPFApplication100.ViewModels
{
    class ViewModel74 : ViewModel
    {
        #region  変更通知プロパティ
        //----------------------------------------------
        public string Parameter { get; set; }
        public string txtMessage { get; set; }
        public string txt本日 { get; set; }
        public string txt状況 { get; set; }
        public string txt区分 { get; set; }
        public string txt年度プロジェクトコード { get; set; }
        public string txtプロジェクト名 { get; set; }
        public string txt作成日 { get; set; }
        public string txt作成者 { get; set; }
        public string txt部名 { get; set; }
        public int txt販売金額 { get; set; }
        public string txt販売先名 { get; set; }
        public string txt検収 { get; set; }
        public int txt仕入金額 { get; set; }
        //----------------------------------------------
        private Project _Project;
        public Project Project
        {
            get { return _Project; }
            private set
            {
                if (_Project == value) return;
                _Project = value;
                RaisePropertyChanged("Project");
            }
        }
        //----------------------------------------------
        #endregion

	    Window win = null;
        public ViewModel74(Project project)
        {
            this.Project = project;

            txtMessage = "Loaded";
            txt本日 = DB.str本日;
            Loaded = new Livet.Commands.ListenerCommand<Window>((w) =>
            {
                if (NeedHideOwner && w.Owner != null && w.Owner.Visibility == 
                	Visibility.Visible)
                {
                    win = w;
                }
            });
            Closing = new Livet.Commands.ListenerCommand<Window>((w) =>
            {
                if (NeedHideOwner && w.Owner != null)
                {
                    w.Owner.Show();
                }
            });
            CopyExecute();
        }
        public bool NeedHideOwner { get; set; }
        public ICommand Loaded { get; private set; }
        public ICommand Closing { get; private set; }

        public void Initialize()
        {
            if (win != null) win.Owner.Hide();
            XCloseButtonManager.Disable();
        }

        public void MouseEnterCommand(object sender, RoutedEventArgs e)
        {
            txtMessage = "MouseEnter";

        }

        private ViewModelCommand _copyCommand;
        public ViewModelCommand CopyCommand
        {
            get
            {
                if (this._copyCommand == null)
                {
                    this._copyCommand = new ViewModelCommand(CopyExecute, 
                    	CanCopyExecute);
                }
                return this._copyCommand;
            }
        }
        
        private void CopyExecute()
        {
            txt状況 = Project.状況;
            txt区分 = Project.区分;
            txt年度プロジェクトコード = Project.年度プロジェクトコード;
            txt部名 = Project.部名;
            txtプロジェクト名 = Project.プロジェクト名;
            txt作成日 = Project.作成日;
            txt作成者 = Project.作成者;

            txt販売金額 = Project.販売金額;

            txt販売先名 = Project.販売先名;
            txt検収 = Project.検収;
            txt仕入金額 = Project.仕入金額;
        }

        private bool CanCopyExecute()
        {
            return true;
        }

        #region CloseCommand
        private ViewModelCommand _CloseCommand;
        public ViewModelCommand CloseCommand
        {
            get
            {
                if (_CloseCommand == null)
                {
                    _CloseCommand = new ViewModelCommand(Close);
                }
                return _CloseCommand;
            }
        }
        public void Close()
        {
            var window = Application.Current.Windows.OfType<Window>().
            	SingleOrDefault((w) => w.IsActive);
            window.Close();
        }
        #endregion

        #region CancelCommand
        private ViewModelCommand _CancelCommand;

        public ViewModelCommand CancelCommand
        {
            get
            {
                if (_CancelCommand == null)
                {
                    _CancelCommand = new ViewModelCommand(Cancel);
                }
                return _CancelCommand;
            }
        }

        public void Cancel()
        {
        }
        #endregion
    }
}