我嘲笑了一種不同於你所建議的方法。這是簡化的,我已經做出了一兩個假設,但讓我們試試這個。
我們不使用網格並將日子匹配到網格中,而是使用WrapPanel,並將其放入每個表示一天的子節點中。
在你的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>
視圖模型
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) });
}
}
現在,我們創造,我們正在創造並增加了對DayViewModel和DayView控制WrapPanel。
<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>
視圖模型
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,它顯示了你。 :)在MainWindow.xaml.cs
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
calendarControl.DataContext =
new CalendarViewModel((IEnumerable<Day>)Application
.Current
.Properties["DaysCollection"]);
}
<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>
代碼隱藏我可能犯了一個錯誤或兩個調換到這裏這從我的解決方案。但是,我希望它能傳達這個想法。這最終會讓我看起來像這樣。
三月日曆
四月日曆
現在,來把所有這些組合起來,使其爲你工作的一部分。這只是演示技術。提出有意義的控制應該不那麼困難。
乾杯。
如果每天只能有一個約會,您將使用哪種控件來存儲約會? 'ListBox'對於一天約會的集合很有用,但我缺少單個約會對象的容器。 ContentControl是我正在尋找的那個? – Boris 2011-03-25 18:29:37
爲什麼你需要一個或另一個?帶有一個項目的「ListBox」應該看起來與其中包含一個項目的「ContentControl」相同。唯一可能分散注意力的是ListBox選擇符,但是當列表中只有一個項目時,可以使用PropertyTrigger和style。但它看起來像你不知道你是否需要每天的約會名單。如果每天只能有一個約會,則不應該使用ItemsControl(或派生類)。在這種情況下,可以使用任何ContentControl派生類。 – 2011-03-25 19:12:47
感謝您澄清戴夫。 – Boris 2011-03-25 19:16:02