私はあなたが提案していたものとは異なるアプローチをモックアップしました。それは単純化されており、私は1つか2つの仮定をしましたが、これを試してみましょう。
グリッドを使用してグリッドに日を一致させる代わりに、WrapPanelを使用して、それぞれが1日を表す子をグリッドに配置してみましょう。
App.xaml.csに、Day
オブジェクトを作成するコードを配置できます。
public class Day
{
public DateTime Date { get; set; }
public List<Appointment> Appointments { get; set; }
}
public partial class App : Application
{
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
var daysCollection = new List<Day>();
for (int i = 1; i <= 30; i++)
{
daysCollection.Add(new Day
{
// arbitrary sample data
Date = new DateTime(2011, 04, i),
Appointments =
new List<Appointment>
{
new Appointment
{
Date = new DateTime(2011, 04, i),
Description = "Some descriptive text"
}
}
});
}
this.Properties.Add("DaysCollection", daysCollection );
}
}
今、私たちは日のコレクションを持っています。サンプルのこの部分では、予定は重要ではありません。
次に、単純なカレンダーUserControlを作成し、CalendarViewModelにバインドします。
<UserControl x:Class="DaysCalendarBinding.Views.Calendar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="210" Width="210">
<WrapPanel x:Name="wrapPanel" Orientation="Horizontal"
ItemHeight="30" ItemWidth="30"
Loaded="wrapPanel_Loaded">
</WrapPanel>
</UserControl>
ViewModel
public class CalendarViewModel
{
public CalendarViewModel()
{
}
public CalendarViewModel(IEnumerable<Day> inputDays)
{
// determine first day of the month passed in
var firstDate =
(from day in inputDays
orderby day.Date.Day
select day.Date).First();
var todayIs = firstDate.DayOfWeek;
var valueOfToday = (int) todayIs;
// create this many blank day children
DaysInMonth = new List<Day>();
for (int i = valueOfToday; i > 0; i--)
{
// the start of some cheeze. I know. It's a sample.
DaysInMonth.Add(new Day { Date = new DateTime(1,1,1) });
}
// add the rest ofthe days in to the collection
foreach(var day in inputDays)
{
DaysInMonth.Add(day);
}
}
public List<Day> DaysInMonth { get; private set; }
}
wrapPanelがロードされたときのイベントハンドラー付き
private void wrapPanel_Loaded(object sender, RoutedEventArgs e)
{
foreach (var day in ((CalendarViewModel)DataContext).DaysInMonth)
{
wrapPanel.Children.Add(
new DayView {
DataContext = new DayViewModel(day) });
}
}
次に、作成してWrapPanelに追加するDayViewModelおよびDayViewコントロールを作成します。
<UserControl x:Class="DaysCalendarBinding.Views.DayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="30">
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel Height="30" Width="30" Background="AliceBlue">
<TextBlock FontSize="7" Text="{Binding DayDate}"/>
</StackPanel>
</Border> </UserControl>
ViewModel
public class DayViewModel
{
private Day innerDay;
public DayViewModel() {}
public DayViewModel(Day day)
{
innerDay = day;
}
public string DayDate
{
get
{
// I know this is a cheesy approach. It's a sample. :)
if (innerDay.Date.Year != 1)
// this only intended to demonstrate some content
return innerDay.Date.DayOfWeek.ToString().Remove(3) +
" " + innerDay.Date.Day;
return string.Empty;
}
}
}
最後に、カレンダーコントロールを追加し、CalendarViewModelを追加するメインウィンドウを追加します。うまくいけば、F5キーを押すと表示されます。:)
<Window x:Class="DaysCalendarBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Views="clr-namespace:DaysCalendarBinding.Views" Title="Calendar Demo" Height="350" Width="525">
<Grid>
<Views:Calendar x:Name="calendarControl"></Views:Calendar>
</Grid>
</Window>
MainWindow.xaml.csのコードビハインド
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
calendarControl.DataContext =
new CalendarViewModel((IEnumerable<Day>)Application
.Current
.Properties["DaysCollection"]);
}
私はこれを私の解決策からここに転置するのを1つか2つ間違えたかもしれません。しかし、それがその考えを伝えてくれることを願っています。これが私にとって最終的にどのように見えるかはこれです。
3月のカレンダー

4月のカレンダー

さて、これをすべてまとめて、あなたのために機能するようにする部分があります。これは単にテクニックを示しています。意味のあるコントロールを提示することはそれほど難しいことではありません。
乾杯。