WPFにおけるWindow同士でのデータのやり取りの一例を紹介したいと思います。
一つはFormでも使ってきたクラスのプロパティとしてのやり方です(ページ2から3へのデータの受け渡し方)。
これは、Page3のmyNameがプロパティです。
Page2で、Page3をインスタンス化して変数に持っているので、Page3のプロパティにアクセスできます。
もう一つはアプリケーションのプロパティですがグローバル変数のようなものです(ページ1から2への受け渡し方)。
これは、サンプルコード内でのApplication.Current.Properties["Name"]になります。
Applicationでプロパティを保持しているので、アプリケーション内のどこからでもアクセスできるので、全体で保持したいデータを扱う時に使うとよいかと思います。
Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Chap2_2
{
/// <summary>
/// Window1.xaml の相互作用ロジック
/// </summary>
public partial class Window1 : NavigationWindow
{
public Window1()
{
InitializeComponent();
Navigate(new Page1());
}
}
}
Page1.xaml
<page x:class="Chap2_2.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
title="ページ1" loaded="Page_Loaded">
<stackpanel>
<label>あなたのお名前は?</label>
<textbox name="_nameBox" />
<textblock>
<hyperlink navigateuri="Page2.xaml" click="link_Click">次へ</hyperlink>
</textblock>
</stackpanel>
</page>
Page1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Chap2_2
{
/// <summary>
/// Page1.xaml の相互作用ロジック
/// </summary>
public partial class Page1 : Page
{
NavigationService _navigation;
public Page1()
{
InitializeComponent();
this.WindowTitle = "ページ1";
}
void link_Click(object sender, RoutedEventArgs e)
{
Page2 p = new Page2();
Application.Current.Properties["Name"] = _nameBox.Text;
_navigation.Navigate(p);
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
//ここで新しいプロパティ"Name"にデータを保存
Application.Current.Properties["Name"] = _nameBox.Text;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
_navigation = this.NavigationService;
_navigation.Navigating += new NavigatingCancelEventHandler(_navigation_Navigating);
_navigation.Navigated += new NavigatedEventHandler(_navigation_Navigated);
}
void _navigation_Navigated(object sender, NavigationEventArgs e)
{
_navigation.Navigated -= this._navigation_Navigated;
_navigation.Navigating -= this._navigation_Navigating;
_navigation = null;
}
void _navigation_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.New && _nameBox.Text.Length == 0)
{
e.Cancel = true;
}
}
}
}
Page2.xaml
<page x:class="Chap2_2.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
title="ページ2" loaded="Page_Loaded">
<stackpanel>
<stackpanel orientation="Horizontal">
<label name="_name" verticalcontentalignment="Top" padding="0" />
<textblock verticalalignment="Stretch">さん、こんにちは。</textblock>
</stackpanel>
<stackpanel orientation="Horizontal">
<textblock>私は、</textblock>
<textbox name="myName" width="50"></textbox>
<textblock>です。</textblock>
</stackpanel>
<textblock><hyperlink navigateuri="Page3.xaml" click="Hyperlink_Click">次へ</hyperlink></textblock>
</stackpanel>
</page>
Page2.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Chap2_2
{
/// <summary>
/// Page2.xaml の相互作用ロジック
/// </summary>
public partial class Page2 : Page
{
NavigationService _navigation;
public Page2()
{
InitializeComponent();
this.WindowTitle = "ぺーじ2";
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
//Page1で保存したデータを取り出す
_name.Content = Application.Current.Properties["Name"];
_navigation = this.NavigationService;
_navigation.Navigating += new NavigatingCancelEventHandler(_navigation_Navigating);
_navigation.Navigated += new NavigatedEventHandler(_navigation_Navigated);
}
void _navigation_Navigated(object sender, NavigationEventArgs e)
{
_navigation.Navigated -= this._navigation_Navigated;
_navigation.Navigating -= this._navigation_Navigating;
_navigation = null;
}
void _navigation_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.New && myName.Text.Length == 0)
{
e.Cancel = true;
}
}
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
Page3 p = new Page3();
//Page3のプロパティ"myName"にデータを保存
p.myName = this.myName.Text;
_navigation.Navigate(p);
}
}
}
Page3.xaml
<page x:class="Chap2_2.Page3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
title="Page3">
<stackpanel orientation="Horizontal">
<textblock>さようなら、</textblock>
<label name="_name" padding="0" />
<textblock>さん。</textblock>
</stackpanel>
</page>
Page3.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Chap2_2
{
/// <summary>
/// Page3.xaml の相互作用ロジック
/// </summary>
public partial class Page3 : Page
{
public Page3()
{
InitializeComponent();
this.WindowTitle = "ページ3";
}
public string myName
{
set { _name.Content = value; }
}
}
}
