チェックボックスの制御を行います。ラジオボタンとの違いはチェックボックス単体で有効、無効の切り替えができる点です。
<phone:PhoneApplicationPage x:Class="BaseApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Canvas x:Name="LayoutRoot" Background="Black"></Canvas> </phone:PhoneApplicationPage>
MainPage.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace BaseApp { // エントリーポイント public partial class MainPage : PhoneApplicationPage { private TextBlock textBlock0 = null; private TextBlock textBlock1 = null; private TextBlock textBlock2 = null; // コンストラクタ public MainPage() { // コンポーネントの初期化。 InitializeComponent(); // 初期化完了後に呼ばれるメソッドの登録。 Loaded += OnLoaded; } // 初期化完了後に呼ばれるメソッド。 void OnLoaded(object sender, RoutedEventArgs args) { // 文字列の表示。 { textBlock0 = new TextBlock(); textBlock0.FontFamily = new FontFamily("MS ゴシック"); textBlock0.FontSize = 32; textBlock0.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)); textBlock0.Text = "check0:OFF"; Canvas.SetLeft(textBlock0, 10); Canvas.SetTop(textBlock0, 10); LayoutRoot.Children.Add(textBlock0); textBlock1 = new TextBlock(); textBlock1.FontFamily = new FontFamily("MS ゴシック"); textBlock1.FontSize = 32; textBlock1.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)); textBlock1.Text = "check1:OFF"; Canvas.SetLeft(textBlock1, 10); Canvas.SetTop(textBlock1, 50); LayoutRoot.Children.Add(textBlock1); textBlock2 = new TextBlock(); textBlock2.FontFamily = new FontFamily("MS ゴシック"); textBlock2.FontSize = 32; textBlock2.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)); textBlock2.Text = "check2:OFF"; Canvas.SetLeft(textBlock2, 10); Canvas.SetTop(textBlock2, 90); LayoutRoot.Children.Add(textBlock2); } // チェックボックスを表示する。 { CheckBox checkbox0 = new CheckBox(); checkbox0.Checked += new RoutedEventHandler(OnButtonChecked); checkbox0.Unchecked += new RoutedEventHandler(OnButtonUnchecked); checkbox0.Name = "check0"; checkbox0.Content = "CheckBox0"; checkbox0.IsChecked = false; Canvas.SetLeft(checkbox0, 100); Canvas.SetTop(checkbox0, 150); LayoutRoot.Children.Add(checkbox0); CheckBox checkbox1 = new CheckBox(); checkbox1.Checked += new RoutedEventHandler(OnButtonChecked); checkbox1.Unchecked += new RoutedEventHandler(OnButtonUnchecked); checkbox1.Name = "check1"; checkbox1.Content = "CheckBox1"; checkbox1.IsChecked = false; Canvas.SetLeft(checkbox1, 100); Canvas.SetTop(checkbox1, 200); LayoutRoot.Children.Add(checkbox1); CheckBox checkbox2 = new CheckBox(); checkbox2.Checked += new RoutedEventHandler(OnButtonChecked); checkbox2.Unchecked += new RoutedEventHandler(OnButtonUnchecked); checkbox2.Name = "check2"; checkbox2.Content = "CheckBox2"; checkbox2.IsChecked = false; Canvas.SetLeft(checkbox2, 100); Canvas.SetTop(checkbox2, 250); LayoutRoot.Children.Add(checkbox2); } } // チェックが付いた時に呼ばれるメソッド。 private void OnButtonChecked(object sender, RoutedEventArgs e) { CheckBox cb = sender as CheckBox; switch (cb.Name) { case "check0": textBlock0.Text = rb.Name + ":ON"; break; case "check1": textBlock1.Text = rb.Name + ":ON"; break; case "check2": textBlock2.Text = rb.Name + ":ON"; break; } } // チェックが外れた時に呼ばれるメソッド。 private void OnButtonUnchecked(object sender, RoutedEventArgs e) { CheckBox cb = sender as CheckBox; switch (cb.Name) { case "check0": textBlock0.Text = rb.Name + ":OFF"; break; case "check1": textBlock1.Text = rb.Name + ":OFF"; break; case "check2": textBlock2.Text = rb.Name + ":OFF"; break; } } } }