前回までの検証で、WPFのウィンドウ(Windowクラス)は、ウィンドウコントロールを、文字列や画像と同様にUI用情報表示用のContent(コンテンツ)で表示することが分かりました。
では、
複数のコントロールを表示するとどうなるのでしょうか?見る前に飛べ、ということで、実際にやってみましょう。
【Window03.cs】
///////////////
// Window03.cs
///////////////
using System;
using System.Windows;
using System.Windows.Controls; //コントロールを利用する為
using System.Windows.Media.Imaging; //BitmapFrameを使用する為
namespace Window03
{
///////////////////////////
//エントリーポイントクラス
///////////////////////////
class MainApp
{
[STAThread]
public static void Main()
{
MainWindow mwnd = new MainWindow();
Application ap = new Application();
ap.Run(mwnd);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
/* ウィンドウスタイル、クライアントエリア色、アイコンとZオーダーの指定 */
this.WindowStyle = WindowStyle.ThreeDBorderWindow; //None、SingleBorderWindow(規定値)、ThreeDBorderWindowまたはToolWindow
this.Background = SystemColors.WindowBrush; //本例ではウインドウのクライアントエリア色にする
this.Icon = BitmapFrame.Create(new Uri("Icon.ico", UriKind.Relative)); //同じフォールダーのIcon.icoを読んで使用する
this.Topmost = false; //最上位ウィンドウとするか否か
/* 起動時の位置-上、左座標で指定または、起動時の位置で指定 */
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
/* ウィンドウサイズ-幅、高さで指定または、コンテンツサイズに合わせる */
this.Width = 640;
this.Height = 480;
//this.Content = "このウィンドウはWPFベースで、プロパティチェックの為に作られました。";
//this.SizeToContent = SizeToContent.WidthAndHeight; //Contentサイズに合わせる(Height、Manual、WidthまたはWidthAndHeight)
this.ResizeMode = ResizeMode.CanResizeWithGrip; //CanMinimize、CanResize、CanResizeWithGrip、NoResizeから選択
this.ShowActivated = true; //アクティブ状態で初期表示するかどうか(規定値 true)
this.ShowInTaskbar = true; //タスクバーにボタン表示するか否か
this.Title = "Window03";
InitControls();
}
public void InitControls()
{
/* ボタンコントロールをつけてみる */
Button btn1 = new Button();
btn1.Content = "I'm Button1. Click me!";
this.Content = btn1;
Button btn2 = new Button();
btn2.Content = "I'm Button2. Click me!";
this.Content = btn2;
Button btn3 = new Button();
btn3.Content = "I'm Button3. Click me!";
this.Content = btn3;
}
}
}
さて、どうなるでしょう?
こうなりましたぁ!
どうも"this.Content ="でbtn1とbtn2が上書きされて、最後のbtn3が生き残ったようです。又、何の指定もなければウィンドウのクライアントエリア全体にストレッチされるようです。次回からはこれをもっと詳しく見て(get more details)行きましょう。