Skip to content

Instantly share code, notes, and snippets.

@biac
Created Dec 9, 2012
Embed
What would you like to do?
カレンダーを表示するコード
var firstDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
int dayOfWeek= (int)firstDate.DayOfWeek; //日曜=0 … 土曜=6
int lastDay = firstDate.AddMonths(1).AddDays(-1).Day;
for (int day = 1; day <= lastDay; day++)
{
int index = (day - 1) + dayOfWeek; // 左上のマスを0番として、左から右へ数えていった番号
int x = index % 7; // column(横方向)位置
int y = index / 7; // row(縦方向)位置
var color = Windows.UI.Colors.White;
if (x == 0) // 日曜日
color = Windows.UI.Colors.Red;
else if (x == 6) // 土曜日
color = Windows.UI.Colors.Cyan;
var fg = new SolidColorBrush(color);
var tb = new TextBlock()
{
Text = string.Format("{0}", day),
FontSize = 40,
Foreground = fg,
};
var fr = new Frame()
{
Content = tb,
BorderBrush = new SolidColorBrush(Windows.UI.Colors.LightSlateGray),
BorderThickness = new Thickness(2.0),
Margin = new Thickness(0.0, 0.0, -2.0, -2.0),
Padding = new Thickness(5.0, 0.0, 5.0, 0.0),
Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 64, 64, 64)),
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch,
};
this.CalendarGrid.Children.Add(fr);
fr.SetValue(Grid.ColumnProperty, x);
fr.SetValue(Grid.RowProperty, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment