概要
郵便番号を入力してボタンを押すと、Web APIを使って郵便番号から住所を取得し、下のテキストボックスに表示します。
プロジェクトの作成
新しいプロジェクトの作成でWPFアプリケーション
を、プロジェクト名:ZipCodeSearch
、フレームワーク:.NET 6.0
で作成してください。
画面の作成
スタイルの作成1
App.xaml
のApplication.Resources
の中に以下のスタイルを定義します。
<!-- 標準的なウィンドウのスタイル -->
<Style x:Key="StandardWindowStyle"
TargetType="Window">
<Setter Property="ResizeMode"
Value="CanResizeWithGrip" />
<Setter Property="FontSize"
Value="16" />
</Style>
<!-- 標準的なラベルのスタイル -->
<Style x:Key="StandardLabelStyle"
TargetType="Label">
<Setter Property="Margin"
Value="10" />
<Setter Property="Padding"
Value="0" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
</Style>
<!-- 標準的なテキストボックスのスタイル -->
<Style x:Key="StandardTextBoxStyle"
TargetType="TextBox">
<Setter Property="Margin"
Value="10" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
</Style>
<!-- 既定のテキストボックススタイル -->
<Style TargetType="TextBox"
BasedOn="{StaticResource StandardTextBoxStyle}" />
<!-- コード入力テキストボックスのスタイル -->
<Style x:Key="CodeTextBoxStyle"
TargetType="TextBox"
BasedOn="{StaticResource StandardTextBoxStyle}">
<!-- 等幅フォント -->
<Setter Property="FontFamily"
Value="Consolas" />
<!-- IME無効 -->
<Setter Property="InputMethod.IsInputMethodEnabled"
Value="False" />
</Style>
<!-- 全角入力テキストボックスのスタイル -->
<Style x:Key="FullWidthCharTextBoxStyle"
TargetType="TextBox"
BasedOn="{StaticResource StandardTextBoxStyle}">
<!-- IMEひらがな -->
<Setter Property="InputMethod.PreferredImeState"
Value="On" />
<Setter Property="InputMethod.PreferredImeConversionMode"
Value="FullShape,Native" />
</Style>
スタイルの作成2
MainWindow.xaml
にWindow.Resources
を作成し、スタイルを定義します。
<Window.Resources>
<!-- 項目ラベルのスタイル -->
<Style x:Key="ItemLabelStyle"
TargetType="Label"
BasedOn="{StaticResource StandardLabelStyle}">
<Setter Property="Foreground"
Value="White" />
</Style>
</Window.Resources>
コントロールの配置・設定
1)
Grid
を行分割します。
後で調整するので大体半分ぐらいで分割します。
Grid
の上の行にStackPanel
を配置します。
StackPanel
の中にさらにStackPanel
を、横並び設定で配置します。
2)
ドキュメントアウトラインで最下層のStackPanel
にツールボックスからLabel
, TextBox
, Button
の順にドラッグ&ドロップします。
3)
TextBox
の名前をZipCode
に設定します。
Text
, TextWrapping
, Width
のリセットボタンを押します。
Margin
(左)を5
に、Margin
(上)を15
に、Margin
(右)を5
に、Margin
(下)を0
に設定します。
MinWidth
に80
を設定します。
MaxLength
に7
を設定します。
Style="{StaticResource CodeTextBoxStyle}"
を設定します。
4)
Window
のプロパティを設定します。
プロパティ | 設定値 |
---|---|
Background | #FFCC0000 |
FocusManager.FocusedElement | {Binding ElementName=ZipCode} |
Style | {StaticResource StandardWindowStyle} |
Title | 郵便番号検索 |
5)
Label
のプロパティを設定します。
プロパティ | 設定値 |
---|---|
Content | 郵便番号 |
Style | {StaticResource ItemLabelStyle} |
Margin(左,上,右,下) | 15,15,5,0 |
6)
ドキュメントアウトラインでButton
の中にGrid
を配置し、
そのGrid
の中にProgressBar
, TextBlock
の順で配置します。
Button
のMargin
に5,15,5,0
を設定します。
ProgressBar
の名前にSearchProgress
を設定し、Width
, Height
をリセットします。
これでプログレスバーがボタン内全体に広がります。
Foreground
に#FF4BB33E
を設定します。
Visibility
にCollapsed
を設定して非表示にします。
IsIndeterminate
にTrue
を設定します。これは「xx% 完了」のような進捗状況ではなく、処理中であることだけを表示したいときに設定します。
BorderThickness
を0
に設定して枠線を消します。
イメージがつきやすいようにd:Visibility
にVisible
を設定して、デザイン時はプログレスバーを表示しておきます。
📘Visibilityプロパティ
Visibility
プロパティは2種類の非表示方法があります。
Collapsed
は非表示にしたコントロールの領域を確保しません。
Hidden
は領域を確保した状態で非表示にします。
Button
の中のTextBlock
のプロパティを設定します。
プロパティ | 設定値 |
---|---|
Text | 住所検索 |
Margin(左右,上下) | 20,0 |
7)
ツールボックスのLabel
をドキュメントアウトラインの最上位のStackPanel
にドラッグ&ドロップします。
プロパティを以下のように設定します。
プロパティ | 設定値 |
---|---|
Content | 住所 |
Margin(左,上,右,下) | 15,15,15,0 |
Style | {StaticResource ItemLabelStyle} |
8)
Grid
の下の行にTextBox
をドラッグ&ドロップします。
TextBox
を右クリックし、レイアウト
> すべてリセット
を選択します。
9)
Grid
の上の行をAuto
に設定します。
10)
住所を表示するTextBox
の名前をResultText
に設定し、以下のプロパティを設定します。
プロパティ | 設定値 |
---|---|
Margin(左,上,右,下) | 15,0,15,15 |
Style | {StaticResource FullWidthCharTextBoxStyle} |
AcceptsReturn | True |
VerticalContentAlignment | Top |
VerticalScrollBarVisibility | Auto |
Text | 空欄 |
11)
Window
のHeight
を250
に設定します。
復習ポイント
- IME制御
- プログレスボタン