Hello World からもう少し進んでWindowに幾つかコントロールを追加する。
Windowsは子どもがひとつしか持てない仕様になっているので、子どもをたくさん持てるパネルコントロールに纏めて配置してから追加する。
パネルコントロールにはGridやStackPanelなど幾つか用意されている。
- // need to add System.Xaml.dll , WindowBase.dll , PresentationCore.dll , PresentationFramework.dll
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls; //PresentationFramework.dll
- namespace WPFFirstPanel
- {
- class Program:Application
- {
- [STAThread]
- static void Main(string[] args)
- {
- new Program().Run(new FirstPanelWindow());
- }
- }
- class FirstPanelWindow : Window
- {
- public FirstPanelWindow()
- {
- InitializeComponent();
- }
- private void InitializeComponent()
- {
- this.Title = "FirstPanel";
- this.Height = 400; this.Width = 400;
- var stPanel = new StackPanel();
- var btn1 = new Button() { Width = Double.NaN, Height = 20, Content = "Hrizontal", Margin = new Thickness(5) };
- var btn2 = new Button() { Width = Double.NaN, Height = 20, Content = "Virtical", Margin = new Thickness(5) };
- var btn3 = new Button() { Width = 200, Height = 20, Content = "btn3", Margin = new Thickness(5) };
- btn1.Click += new RoutedEventHandler((o, e) => { stPanel.Orientation = Orientation.Horizontal; });
- btn2.Click += new RoutedEventHandler((o, e) => { stPanel.Orientation = Orientation.Vertical; });
- stPanel.Children.Add(btn1); stPanel.Children.Add(btn2); stPanel.Children.Add(btn3);
- this.AddChild(stPanel);
- this.SizeToContent = SizeToContent.WidthAndHeight;
- }
- }
- }
--
<キーワード>
コードでAutoサイズを指定する
画面サイズを中身のサイズに合わせる
0 件のコメント:
コメントを投稿