WPF入門してみたで書いた内容をxamlで書いてみようと思う。
とりあえず、WPFアプリケーションを新規作成。名前はHelloWPF2という名前にした。
Window1.xamlが出来てるので、折角だからこれを使わせてもらうことにする。
デフォで中身は、こんな感じになってる。
<Window x:Class="HelloWPF2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
</Grid>
</Window>
x:で始まる属性で、cs方面に伝える情報を書いてるっぽい。
さっきのサンプルだと、Titleに「こんにちはWPF」、Heightに「225」、Widthに「250」を設定してたので、下のように書き換え。
<Window x:Class="HelloWPF2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="こんにちはWPF" Height="225" Width="250">
<Grid>
</Grid>
</Window>
後は、ボタンを置くだけだ!!!今回は、Gridは邪魔なので消してButtonタグをかわりに書く。
Buttonタグには、x:Name属性でcs側からアクセスする際の変数名を記述する。
後は、Contentに「クリック!」、Clickイベントに「button_Click」を設定すればOK。
<Window x:Class="HelloWPF2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="こんにちはWPF" Height="225" Width="250">
<Button x:Name="button" Content="クリック!" Click="button_Click" />
</Window>
Window1.xaml.csファイルに、button_Clickメソッドが出来てるので「this.Title = "今は夜だよ?";」を足してあげる。
実行すると、前にC#のコードで書いたのと同じ動きをするはず!!