C# WPF Tips

Windowsフォームプログラミング、WPFプログラミングのTipsを紹介します。極力、実務に役立つように作成しました


7.ListBoxにWクリック実装

戻る

ListBox

ListBoxにWクリック機能を実装し引数を伴なって画面遷移をする例です。画面は下記の通りです。


行をクリックすると、その行の内容がテキストボックスに表示されます。

Wクリックしますと、テキストボックスの内容を引数にして別の画面へ遷移します。

クリックすると拡大します

遷移先の画面で、引数の内容をテキストボックスにコピーします。


画遷移先の画面

クリックすると拡大します


下記が遷移元のZAMLです。


View(遷移元)

<ccl:CustomChromeWindow 
        x:Class="LivetWPFChromeHelpDesk1.Views.Window13"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
        xmlns:ccl="clr-namespace:CustomChromeLibrary;assembly=CustomChromeLibrary"                                  
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
        xmlns:core="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"      
        xmlns:v="clr-namespace:LivetWPFChromeHelpDesk1.Views"
        xmlns:vm="clr-namespace:LivetWPFChromeHelpDesk1.ViewModels"    
        WindowStartupLocation="CenterScreen"
        Title="Window13" Height="714" Width="1035">

    <shell:WindowChrome.WindowChrome>
        <shell:WindowChrome
            ResizeBorderThickness="6"
            CaptionHeight="43"
            CornerRadius="0,0,0,0"
            GlassFrameThickness="0">
        </shell:WindowChrome>
    </shell:WindowChrome.WindowChrome>

    <Window.Resources>
        <ResourceDictionary>
            <vm:XComboBoxEmptyItemConverter x:Key="XComboBoxEmptyItemConverter"/>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Styles.xaml"/>
                <ResourceDictionary Source="/Resources/Styles2.xaml"/>
                <ResourceDictionary Source="/Resources/StylesBG.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

遷移元のViewModelは下記の通りです。


ViewModel(遷移元)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel;

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

//ICommand
using System.Windows.Input;

using System.Data.OracleClient;

// ListCollectionView
using System.Windows.Data;

//MessageBox
using System.Windows;

using LivetWPFChromeHelpDesk1.Models;
using LivetWPFChromeHelpDesk1.Views;
using LivetWPFChromeHelpDesk1.ViewModels;

namespace LivetWPFChromeHelpDesk1.ViewModels
{
    class ViewModel13 : ViewModel
    {
        #region 変更通知プロパティ
        //-----------------------------------------------
        private string _txtMessage;
        public string txtMessage
        {
            get { return _txtMessage; }
            set
            {
                _txtMessage = value;
                RaisePropertyChanged("txtMessage");
            }
        }

        public string txt本日 { get; set; }

        private string _SelectedValue;
        public string SelectedValue
        {
            get
            {
                return _SelectedValue;
            }
            set
            {
                if (value == _SelectedValue)
                    return;

                _SelectedValue = value;
                RaisePropertyChanged("SelectedValue");

                //  EstimateGetData();
            }
        }
        //-----------------------------------------------
        #endregion

        Window win = null;
        public ViewModel13()
        {
            Loaded = new Livet.Commands.ListenerCommand<Window>((w) =>
            {
                if (NeedHideOwner && w.Owner != null && w.Owner.Visibility == Visibility.Visible)
                {
                    win = w;
                    //w.Owner.Hide();
                }
            });
/*
            Closing = new Livet.Commands.ListenerCommand<Window>((w) =>
            {
                if (NeedHideOwner && w.Owner != null)
                {
                    w.Owner.Show();
                }
            });
*/
            //Initialize()では表示されない
            txt本日 = Convert.ToString(DateTime.Today.ToShortDateString());
        }
        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();

            //★ListCollectionView 5-2
            this.StaffList = new List<string>();

            GetPersonData();

            //★ListCollectionView 5-4
            this.StaffListView = new ListCollectionView(this.StaffList);
            this.StaffListView.CurrentChanged += StaffListView_CurrentChanged;
            //下記のRefreshでListBoxへバインドを実行させます
            this.StaffListView.Refresh();
        }


        #region StaffListView 社員一覧(ListBox)
        //★ListCollectionView 5-1
        private List<string> StaffList;

        /// <summary>
        /// 社員一覧(ListBox)
        /// </summary>
        private ListCollectionView _StaffListView;
        public ListCollectionView StaffListView
        {
            get
            { return _StaffListView; }
            set
            {
                if (_StaffListView == value)
                    return;
                _StaffListView = value;
                RaisePropertyChanged("StaffListView");
            }
        }

        void StaffListView_CurrentChanged(object sender, EventArgs e)
        {
            this.StaffListView.Refresh();
            //
            var lv = sender as ICollectionView;
            if (lv.CurrentPosition < 0)
            {
                System.Diagnostics.Trace.WriteLine("選択無し");
                return;
            }
            txtMessage = lv.CurrentItem as string;
        }
        #endregion

        #region GetPersonData
        public void GetPersonData()
        {
            this.StaffList.Add("");
            this.StaffList.Add("小堺 一機");
            this.StaffList.Add("柳葉 敏郎");
            this.StaffList.Add("吉田 栄作");
            this.StaffList.Add("長井 秀和");
            this.StaffList.Add("竹内 力");
            this.StaffList.Add("宮崎 駿");
            this.StaffList.Add("小池 徹平");
            this.StaffList.Add("高橋 由美子");
            this.StaffList.Add("岡本 真夜");
            this.StaffList.Add("山口 達也");
        }
        #endregion

        #region GotoCommand        
        //---------------------------------------------------------------
        public ViewModelCommand GotoCommand14
        {
            get { return new Livet.Commands.ViewModelCommand(Goto14); }
        }
        public void Goto14()
        {
            Person person = new Person();
            person.Name = txtMessage;

            Messenger.Raise(new TransitionMessage(new ViewModel14(person) { NeedHideOwner = true }, "MessageKey14"));
        }
        //----------------------------------------------------------------       
        #endregion
    }
}

下記が遷移先のZAMLの定義です。引数のある画面遷移では、34~38行目のWindow.DataContextを付けるとエラーになりますのでコメントにしています。
InitializeメソッドとActivatedメソッドは、どちらか一方を使います。
ここでは、Initializeメソッドを使っていますので、Activatedをコメントにしています。
遷移先でデータを更新して、遷移元に戻って来た時に、その内容を画面に反映させる場合は、Activatedメソッドを使います。


View(遷移先)

<ccl:CustomChromeWindow 
        x:Class="LivetWPFChromeHelpDesk1.Views.Window14"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
        xmlns:ccl="clr-namespace:CustomChromeLibrary;assembly=CustomChromeLibrary"                                  
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
        xmlns:core="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"      
        xmlns:v="clr-namespace:LivetWPFChromeHelpDesk1.Views"
        xmlns:vm="clr-namespace:LivetWPFChromeHelpDesk1.ViewModels"    
        WindowStartupLocation="CenterScreen"
        Title="Window14" Height="714" Width="1035">

    <shell:WindowChrome.WindowChrome>
        <shell:WindowChrome
            ResizeBorderThickness="6"
            CaptionHeight="43"
            CornerRadius="0,0,0,0"
            GlassFrameThickness="0">
        </shell:WindowChrome>
    </shell:WindowChrome.WindowChrome>

    <Window.Resources>
        <ResourceDictionary>
            <vm:XComboBoxEmptyItemConverter x:Key="XComboBoxEmptyItemConverter"/>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Styles.xaml"/>
                <ResourceDictionary Source="/Resources/Styles2.xaml"/>
                <ResourceDictionary Source="/Resources/StylesBG.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
<!--
    <Window.DataContext>
        <vm:ViewModel14/>
    </Window.DataContext>
-->    
    <i:Interaction.Triggers>

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

        <!-- 下記がないと、タスクバーが1つにならない -->
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding Path=Loaded}" 
                                   CommandParameter="{Binding Mode=OneTime,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
        </i:EventTrigger>

        <!--  Activatedを設定する -->
        <!--
        <i:EventTrigger EventName="Activated" >
            <i:InvokeCommandAction Command="{Binding Activated}" />
        </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-1 -->
    <Grid>

        <!-- WindowChrome Start  -->
        <Border  Grid.RowSpan="2" BorderThickness="3" BorderBrush="Black">
            <Border.Background>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="Black" Offset="1" />
                </LinearGradientBrush>
            </Border.Background>
        </Border>

        <Border  BorderThickness="3,3,3,1" BorderBrush="Black" Margin="{Binding Path=CaptionButtonMargin}">
            <Border.Background>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="Black" Offset="1" />
                </LinearGradientBrush>
            </Border.Background>
            <!--Window Icon and Title-->
            <StackPanel Orientation="Horizontal" Margin="0" VerticalAlignment="Top">
                <TextBlock Text=" Help Desk" FontFamily="Calibri" FontWeight="Bold" FontSize="26" Foreground="Blue" />
            </StackPanel>
        </Border>
        <ccl:CaptionButtons />
        <!-- WindowChrome End  -->

        <!-- Grid-2 -->
        <!--Content-->
        <Grid Grid.Row="1">

            <!-- ★★★ -->
            <Grid>
                <TextBox Text="{Binding Path=txt本日}" TextAlignment="Center" 
                    Foreground="White" Background="Black"
                    Height="17" HorizontalAlignment="Center"  Name="txt本日" 
                    VerticalAlignment="Top" Width="81" Margin="809,3,123,0" />

                <TextBox Text="{Binding Path=txtMessage, Mode=TwoWay}"  
                        Foreground="White" Background="Black" Height="24" 
                        VerticalContentAlignment="Center" HorizontalAlignment="Left" 
                        VerticalAlignment="Top" Width="160" Margin="277,87,0,0" />


            </Grid>
            <!-- ★★★ -->

        </Grid>
    </Grid>
</ccl:CustomChromeWindow>

遷移先のViewModelは下記の通りです。


ViewModel(遷移先)

using System;
using System.Collections.Generic;
using System.Linq;

//INotifyPropertyChanged
//PropertyChanged
using System.ComponentModel;

//参照設定が必要
//using System.Configuration;

using Livet;
using Livet.Commands;
using Livet.Messaging;
//CloseCommand
using Livet.Messaging.Windows;

//using Livet.Messaging.IO;
//using Livet.EventListeners;

using System.Data.OracleClient;

//ArrayList
using System.Collections;

//XmlReader
using System.Xml;

//XDocument
using System.Xml.Linq;

//XPathDocument
using System.Xml.XPath;

//RequestNavigateEventArgs
//Process.Start
using System.Diagnostics;


//SystemCommands
using Microsoft.Windows.Shell;

//ICommand
using System.Windows.Input;

//MessageBox
using System.Windows;

// ListCollectionView
using System.Windows.Data;

//Encoding
using System.Text;

using LivetWPFChromeHelpDesk1.Models;
using LivetWPFChromeHelpDesk1.Views;
using LivetWPFChromeHelpDesk1.ViewModels;

namespace LivetWPFChromeHelpDesk1.ViewModels
{
    class ViewModel14 : ViewModel
    {
        #region 変更通知プロパティ
        //-----------------------------------------------
        private Argument _person;
        public Argument person
        {
            get { return _person; }
            set
            {
                if (_person == value)
                    return;
                _person = value;
                RaisePropertyChanged("person");
            }
        }
        //
        private string _txtMessage;
        public string txtMessage
        {
            get { return _txtMessage; }
            set
            {
                _txtMessage = value;
                RaisePropertyChanged("txtMessage");
            }
        }
        //
        public string txt本日 { get; set; }
        //-----------------------------------------------
        #endregion

       Window win = null;
       public ViewModel14(Person person)
       {
            Loaded = new Livet.Commands.ListenerCommand((w) =>
            {
                if (NeedHideOwner && w.Owner != null && w.Owner.Visibility == Visibility.Visible)
                {
                    win = w;
                    //w.Owner.Hide();
                }
            });
/*
            Closing = new Livet.Commands.ListenerCommand((w) =>
            {
                if (NeedHideOwner && w.Owner != null)
                {
                    w.Owner.Show();
                }
            });
*/
            //Initialize()では表示されない  
            txt本日 = Convert.ToString(DateTime.Today.ToShortDateString());
            txtMessage = person.Name;
        }
        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();
        }

        #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().SingleOrDefault((w) => w.IsActive);
            window.Close();
        }
        #endregion
    }
}