present@わんくま

本家はこちら

目次

Blog 利用状況

ニュース

アクセサリ

書庫

日記カテゴリ

ギャラリ

リンク

WPF のレイアウトパネルを使ってみた

本格的に WPF に挑戦!

Tech-Ed に行ってから、無性に WPF が触りたくなりました。
教科書を適当に見繕って、れっつ・ちゃれんじ。

まずは実行画面

最初に結果をお見せします。

PC にインストールしているランチャの設定画面をモデルにしました。

XAML を晒す

上図のようなウィンドウを作成するために苦労して書いたのが下のコード。

<Window x:Class="WpfSample.EditDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EditDialog" Height="400" Width="400">
    <DockPanel LastChildFill="True">
        <StackPanel DockPanel.Dock="Bottom" Height="30" Orientation="Horizontal" FlowDirection="RightToLeft">
            <Button Width="100" Content="キャンセル"/>
            <Button Width="100" Content="OK"/>
        </StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="50" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <Label Grid.Column="0" Grid.Row="0" Content="名前" />
            <Label Grid.Column="0" Grid.Row="1" Content="ファイル名" />
            <Label Grid.Column="0" Grid.Row="2" Content="パラメータ" />
            <Label Grid.Column="0" Grid.Row="3" Content="作業フォルダ"/>
            <Label Grid.Column="0" Grid.Row="4" Content="実行時の大きさ"/>
            <Label Grid.Column="0" Grid.Row="5" Content="ツールチップ"/>
            <Label Grid.Column="0" Grid.Row="6" Content="ショートカットキー" />
            <Label Grid.Column="0" Grid.Row="7" Content="ホットキー"/>
            <TextBox Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2"/>
            <TextBox Grid.Column="1" Grid.Row="1"/>
            <TextBox Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2"/>
            <TextBox Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="2"/>
            <ComboBox Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" SelectedIndex="0">
                <ComboBoxItem Content="通常のウィンドウ" />
                <ComboBoxItem Content="最大化" />
                <ComboBoxItem Content="最小化" />
            </ComboBox>
            <TextBox Grid.Column="1" Grid.Row="5" Grid.ColumnSpan="2" />
            <TextBox Grid.Column="1" Grid.Row="6" />
            <TextBox Grid.Column="1" Grid.Row="7" />
            <Button Grid.Column="2" Grid.Row="1" Content="参照"/>
            <Button Grid.Column="2" Grid.Row="6" Content="クリア"/>
            <Button Grid.Column="2" Grid.Row="7" Content="クリア"/>
        </Grid>
    </DockPanel>
</Window>

今回の方針として、コントロールの配置はすべてレイアウトパネルで行っています。

  • 入力欄はきれいに位置を揃えたいので Grid
  • OK とキャンセルのボタンは StackPanel で右から左へ順に並べる
  • この2つのレイアウトパネルを DockPanel に乗っけて位置関係を指定

作ったサンプルはイベント処理とか全く記述していません。レイアウトのみ。これだけでも1時間以上かかりました。

一番工夫した部分は、Grid の Button が無い行に ColumnSpan を指定して、2列目と3列目を結合したところかな。「大した工夫じゃないよw」というツッコミはご勘弁を。

最後に

次は Style に挑戦予定。ちなみに今回は Expression Blend や Visual Studio のデザイナなんて使っていません。

漢は黙って、XAML を直書き!

投稿日時 : 2008年9月11日 21:46

コメントを追加

# re: WPF のレイアウトパネルを使ってみた 2008/09/11 22:16 Hirotow

StyleでTextBoxの高さや余白を設定しとくとRowDefinitionにHeightとか書かなくてすみますよ。
あとTextBlockのHorizontalAlignmentとVerticalAlignmentもStyleで設定しとくといいです。

# re: WPF のレイアウトパネルを使ってみた 2008/09/11 23:15 なかむら

>Hirotow さん
アドバイスありがとう!
Style では Margin や Background も弄る予定です。
というか、現在挑戦中~。

# re: WPF のレイアウトパネルを使ってみた 2008/09/12 8:26 Jitta

そこは「漢」を使わないと。

# re: WPF のレイアウトパネルを使ってみた 2008/09/12 8:45 なかむら

>そこは「漢」を使わないと。
指摘されるまで、その字の存在を忘れてました><
「おとこ→男」としか変換されなくて…。

早速、「男」から「漢」に訂正しました。
(今回は、あえて<del>タグで打ち消しませんでした。これって隠蔽?)

# re: WPF のレイアウトパネルを使ってみた 2008/09/12 17:58 かずき

XAML手打ちなか~ま
タグ手打ちで基礎を磨くのがステキですよね♪

最近やっつけだとデザイナを使うようになってきたりてますけど…orz
軟弱になりつつある。

# WPF の Style に挑戦 2008/09/13 1:21 Nakamura Blog

WPF の Style に挑戦

# We stumbled over here different web address and thought I might as well check things out. I like what I see so i am just following you. Look forward to looking at your web page for a second time. 2019/05/13 18:47 We stumbled over here different web address and t

We stumbled over here different web address and thought I
might as well check things out. I like what I see so i
am just following you. Look forward to looking at
your web page for a second time.

# Heya i'm for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you aided me. 2019/05/18 2:42 Heya i'm for the first time here. I came across th

Heya i'm for the first time here. I came across this board and I find It really useful & it helped me out much.
I hope to give something back and aid others like you aided me.

# erectile chambers 2021/07/06 22:55 hydroxychlor side effects

difference between chloroquine phosphate and hydroxychloroquine https://plaquenilx.com/# hydroxychoroquine

# re: WPF ??????????????? 2021/07/08 11:11 plaquenil 400 mg

cloraquine https://chloroquineorigin.com/# hydroxychoriquine

# re: WPF ??????????????? 2021/07/14 14:39 hydroxychloroquine 200 mg

chloroquine tablets https://chloroquineorigin.com/# lupus usmle

# re: WPF ??????????????? 2021/08/08 20:50 hydroxychlor 200 mg

anti-malaria drug chloroquine https://chloroquineorigin.com/# hydroxycloro

# aWVYPgeGehdaRUkij 2022/04/19 11:53 markus

http://imrdsoacha.gov.co/silvitra-120mg-qrms

# qcsosbmyhyns 2022/05/28 15:54 xlivbzhz

https://erythromycinn.com/# erythromycin ophthalmic ointment usp 0.5

# Test, just a test 2022/12/13 4:51 http://candipharm.com

canadian generic pills http://candipharm.com

# chloroquine tablets dosage 2022/12/27 8:34 MorrisReaks

http://www.hydroxychloroquinex.com/# aralen tablets

タイトル
名前
URL
コメント