QA@IT

Silverlightアプリケーションでカレンダーアプリを作成しています。わからないことがあり、よろしくお願いします。

2180 PV

Win8,vs2012,c#で開発しています。ブラウザに出力するカレンダーを作成し、カレンダーの出力はできました。で囲っている日付をクリックすると、別の画面に移動し日付ごとに予定を記入できるようにしたいのです。しかし、で囲まれた数字にリンクを張ったり、数字をクリックするとイベントが発生するにはC#でコードをどのように書けばよろしいのでしょうか。よろしくお願いします。

//C#で日付を表示するために必要なtextblockを作成しています。
int r = i / 7 + 1;
int c = i % 7;
Rectangle rect = new Rectangle();
rect.Height = 90;
rect.Width = 90;

Grid.SetColumn(rect, c);
Grid.SetRow(rect, r);

TextBlock text = new TextBlock();
text.Height = 90;
text.Width = 90;
text.TextAlignment = TextAlignment.Center;
text.FontSize = 70;

Grid.SetColumn(text, c);
Grid.SetRow(text, r);

if (i < 35)
  gridMain.Children.Add(rect);

calendarRectangles.Add(rect);
gridMain.Children.Add(text);
calendarTexts.Add(text);

こちらは、プログラム全体のソースです。
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.Shapes;

namespace calenderSample2
{
    public partial class MainPage : UserControl
    {
        List<TextBlock> calendarTexts = new List<TextBlock>();
        List<Rectangle> calendarRectangles = new List<Rectangle>();

        int year, month;

        public MainPage()
        {
            InitializeComponent();

            MakeShapes();
            year = DateTime.Now.Year;
            month = DateTime.Now.Month;
            DispCalendar(year, month);
        }

        private void MakeShapes()
        {
            for (int i = 0; i < 37; i++)
            {
                int r = i / 7 + 1;
                int c = i % 7;
                Rectangle rect = new Rectangle();
                rect.Height = 90;
                rect.Width = 90;

                Grid.SetColumn(rect, c);
                Grid.SetRow(rect, r);

                TextBlock text = new TextBlock();
                text.Height = 90;
                text.Width = 90;
                text.TextAlignment = TextAlignment.Center;
                text.FontSize = 70;

                Grid.SetColumn(text, c);
                Grid.SetRow(text, r);

                if (i < 35)
                    gridMain.Children.Add(rect);

                calendarRectangles.Add(rect);
                gridMain.Children.Add(text);
                calendarTexts.Add(text);

            }
        }

        void DispCalendar(int y, int m)
        {
            DateTime firstMonth = new DateTime(y, m, 1);
            DateTime currentDate =
                firstMonth.AddDays(-(int)(firstMonth.DayOfWeek));
            textBlock1.Text = string.Format("{0:yyyy年MM月}", firstMonth);
            int x = 0;
            for (int i = 0; i < calendarTexts.Count; i++)
            {
                string rectStyle, textStyle;

                if (m == currentDate.Month)
                {
                    // 今月分
                    rectStyle = IsSun(currentDate) ? "DateSunRectStyle" :
                        IsSat(currentDate) ? "DateSatRectStyle" : "DateDayRectStyle";
                    if (i < 35)
                        textStyle = IsSun(currentDate) ? "DateSunTextStyle" :
                            IsSat(currentDate) ? "DateSatTextStyle" : "DateDayTextStyle";
                    else
                        textStyle = IsSun(currentDate) ? "PackSunTextStyle" : "PackDayTextStyle";
                }
                else
                {
                    // 前月または翌月
                    rectStyle = IsSun(currentDate) ? "WhiteSunRectStyle" :
                        IsSat(currentDate) ? "WhiteSatRectStyle" : "WhiteDayRectStyle";
                    textStyle = IsSun(currentDate) ? "WhiteSunTextStyle" :
                        IsSat(currentDate) ? "WhiteSatTextStyle" : "WhiteDayTextStyle";
                }

                calendarRectangles[i].Style = this.Resources[rectStyle] as Style;
                calendarTexts[i].Style = this.Resources[textStyle] as Style;

                if (i < 35 || i > 34 && m == currentDate.Month)
                    calendarTexts[i].Text = currentDate.Day.ToString();
                else
                    calendarTexts[i].Text = "";

                if (firstMonth.Month != currentDate.Month)
                {
                    calendarTexts[i].Text = "";
                }

                currentDate = currentDate.AddDays(1);
                x += 1;
            }

        }

        private bool IsSun(DateTime dt)
        {
            return (dt.DayOfWeek == DayOfWeek.Sunday);
        }

        private bool IsSat(DateTime dt)
        {
            return (dt.DayOfWeek == DayOfWeek.Saturday);
        }

        private void btnLight_Click(object sender, RoutedEventArgs e)
        {
            if (month == 1)
            {
                month = 12;
                year--;
            }
            else
                month--;

            DispCalendar(year, month);
        }

        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            if (month == 12)
            {
                month = 1;
                year++;
            }
            else
                month++;

            DispCalendar(year, month);
        }

    }
}

MainPage.xaml

<UserControl x:Class="calenderSample2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="750" d:DesignWidth="1000">

    <UserControl.Resources>
        <Style x:Key="BaseRectStyle" TargetType="Rectangle">
            <Setter Property="StrokeThickness" Value="0" />
            <Setter Property="Margin" Value="1,1,0,0" />
        </Style>

        <Style x:Key="DateSunRectStyle"
            BasedOn="{StaticResource BaseRectStyle}"
            TargetType="Rectangle">
            <Setter Property="Fill">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="#FFD50019" />
                        <GradientStop Offset="1" Color="#FFF05258" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="DateSatRectStyle" BasedOn="{StaticResource BaseRectStyle}" TargetType="Rectangle">
            <Setter Property="Fill">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="#FF006CC9" />
                        <GradientStop Offset="1" Color="#FF008DF2" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="DateDayRectStyle" BasedOn="{StaticResource BaseRectStyle}" TargetType="Rectangle">
            <Setter Property="Fill">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="#FF808080" />
                        <GradientStop Offset="1" Color="#FFB2B2B2" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="WhiteSunRectStyle" TargetType="Rectangle"
            BasedOn="{StaticResource BaseRectStyle}">
            <Setter Property="Fill" Value="Transparent" />
        </Style>

        <Style x:Key="WhiteDayRectStyle" TargetType="Rectangle" BasedOn="{StaticResource WhiteSunRectStyle}" />
        <Style x:Key="WhiteSatRectStyle" TargetType="Rectangle" BasedOn="{StaticResource WhiteSunRectStyle}" />

        <Style x:Key="BaseTextStyle" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="Arial" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="FontSize" Value="40" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Top" />
            <Setter Property="Foreground" Value="#FFFFFF" />
        </Style>

        <Style x:Key="TitleSunTextStyle" TargetType="TextBlock"
            BasedOn="{StaticResource BaseTextStyle}">
            <Setter Property="FontSize" Value="40" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>

        <Style x:Key="TitleSatTextStyle" TargetType="TextBlock" BasedOn="{StaticResource TitleSunTextStyle}" />
        <Style x:Key="TitleDayTextStyle" TargetType="TextBlock" BasedOn="{StaticResource TitleSunTextStyle}" />

        <Style x:Key="DateSunTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseTextStyle}" />
        <Style x:Key="DateSatTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseTextStyle}" />
        <Style x:Key="DateDayTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseTextStyle}" />

        <Style x:Key="WhiteSunTextStyle" TargetType="TextBlock" BasedOn="{StaticResource DateSunTextStyle}" >
            <Setter Property="FontSize" Value="40" />
            <Setter Property="Foreground" Value="#FFD50019" />
        </Style>

        <Style x:Key="WhiteSatTextStyle" TargetType="TextBlock" BasedOn="{StaticResource WhiteSunTextStyle}" >
            <Setter Property="Foreground" Value="#FF006CC9" />
        </Style>

        <Style x:Key="WhiteDayTextStyle" TargetType="TextBlock" BasedOn="{StaticResource WhiteSunTextStyle}" >
            <Setter Property="Foreground" Value="#FF808080" />
        </Style>

        <Style x:Key="PackSunTextStyle" TargetType="TextBlock" BasedOn="{StaticResource DateSunTextStyle}">
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
        </Style>

        <Style x:Key="PackDayTextStyle" TargetType="TextBlock" BasedOn="{StaticResource DateDayTextStyle}">
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid Grid.Row="0" HorizontalAlignment="Center" Margin="8" Name="grid" VerticalAlignment="Top" Width="800">
            <StackPanel Name="stackpnel1" Margin="8" HorizontalAlignment="Center" Orientation="Horizontal" VerticalAlignment="Top">
                <Button Content="&lt;" Margin="4" Height="50" Width="50" Name="btnLight" Click="btnLight_Click"/>
                <TextBlock Name="textBlock1" HorizontalAlignment="Center" Margin="4" Text="勉強用自作カレンダー" VerticalAlignment="Center"  Height="30" Width="500" FontSize="24" TextAlignment="Center"/>
                <Button Content="&gt;" Margin="4" Width="50" Height="50" Name="btnRight" Click="btnRight_Click"/>
            </StackPanel>
        </Grid>

        <Grid HorizontalAlignment="Center" Margin="35,100,35,-2" Name="gridMain" VerticalAlignment="Top" Width="1000">
            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="100" />
                <RowDefinition Height="100" />
                <RowDefinition Height="100" />
                <RowDefinition Height="100" />
                <RowDefinition Height="100" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Rectangle Fill="#FFF4F4F5" Grid.Column="0" Height="30" Width="100"/>
            <Rectangle Fill="#FFF4F4F5" Grid.Column="1" Height="30" Width="100"/>
            <Rectangle Fill="#FFF4F4F5" Grid.Column="2" Height="30" Width="100"/>
            <Rectangle Fill="#FFF4F4F5" Grid.Column="3" Height="30" Width="100"/>
            <Rectangle Fill="#FFF4F4F5" Grid.Column="4" Height="30" Width="100"/>
            <Rectangle Fill="#FFF4F4F5" Grid.Column="5" Height="30" Width="100"/>
            <Rectangle Fill="#FFF4F4F5" Grid.Column="6" Height="30" Width="100"/>
            <TextBlock Grid.Column="0" Text="日" Height="40" Width="100" FontSize="30" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            <TextBlock Grid.Column="1" Text="月" Height="40" Width="100" FontSize="30" TextAlignment="Center"/>
            <TextBlock Grid.Column="2" Text="火" Height="40" Width="100" FontSize="30" TextAlignment="Center"/>
            <TextBlock Grid.Column="3" Text="水" Height="40" Width="100" FontSize="30" TextAlignment="Center"/>
            <TextBlock Grid.Column="4" Text="木" Height="40" Width="100" FontSize="30" TextAlignment="Center"/>
            <TextBlock Grid.Column="5" Text="金" Height="40" Width="100" FontSize="30" TextAlignment="Center"/>
            <TextBlock Grid.Column="6" Text="土" Height="40" Width="100" FontSize="30" TextAlignment="Center"/>
        </Grid>
    </Grid>
</UserControl>


ウォッチ

この質問への回答やコメントをメールでお知らせします。