ページ

2011年10月26日水曜日

◆WPF パネルを使ってコントロールを幾つか追加する

レイアウトに挑戦

Hello World からもう少し進んでWindowに幾つかコントロールを追加する。
Windowsは子どもがひとつしか持てない仕様になっているので、子どもをたくさん持てるパネルコントロールに纏めて配置してから追加する。

パネルコントロールにはGridやStackPanelなど幾つか用意されている。

  1. // need to add System.Xaml.dll , WindowBase.dll , PresentationCore.dll , PresentationFramework.dll  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6.   
  7. using System.Windows;             
  8. using System.Windows.Controls;  //PresentationFramework.dll  
  9.   
  10. namespace WPFFirstPanel  
  11. {  
  12.     class Program:Application  
  13.     {  
  14.         [STAThread]  
  15.         static void Main(string[] args)  
  16.         {  
  17.             new Program().Run(new FirstPanelWindow());  
  18.         }  
  19.   
  20.     }  
  21.   
  22.     class FirstPanelWindow : Window  
  23.     {  
  24.         public FirstPanelWindow()  
  25.         {  
  26.             InitializeComponent();  
  27.         }  
  28.   
  29.         private void InitializeComponent()  
  30.         {  
  31.             this.Title = "FirstPanel";  
  32.             this.Height = 400; this.Width = 400;  
  33.             var stPanel = new StackPanel();  
  34.             var btn1 = new Button() { Width = Double.NaN, Height = 20, Content = "Hrizontal", Margin = new Thickness(5) };  
  35.             var btn2 = new Button() { Width = Double.NaN, Height = 20, Content = "Virtical", Margin = new Thickness(5) };  
  36.             var btn3 = new Button() { Width = 200, Height = 20, Content = "btn3", Margin = new Thickness(5) };  
  37.             btn1.Click += new RoutedEventHandler((o, e) => { stPanel.Orientation = Orientation.Horizontal; });  
  38.             btn2.Click += new RoutedEventHandler((o, e) => { stPanel.Orientation = Orientation.Vertical; });  
  39.               
  40.             stPanel.Children.Add(btn1); stPanel.Children.Add(btn2); stPanel.Children.Add(btn3);  
  41.             this.AddChild(stPanel);  
  42.   
  43.             this.SizeToContent = SizeToContent.WidthAndHeight;  
  44.         }  
  45.     }  
  46.   
  47.   
  48. }  

--


<キーワード>
コードでAutoサイズを指定する
画面サイズを中身のサイズに合わせる

0 件のコメント:

コメントを投稿

私が最近チェックした記事