WPF monthly View Control and wpf View Control
Introduction
When you create an application, you need to create a calendar month view. I had trouble doing it myself, so I went online and looked for it. On CodeProject, I found this Quick and Simple WPF Month-view Calendar, but the background of the program was written in VB. Although I don't know how to write VB, I can understand it as well. So I changed it to C # and made some improvements to make it more universal. As follows:
<Window x: Class = "MonthViewTest. mainWindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: monthControl =" clr-namespace: MonthView. controls; assembly = MonthView "Title =" MainWindow "Width =" 800 "Height =" 600 "> <monthControl: MonthControl x: Name =" TestMonthControl "/> </Window>
Background code usage:
Using MonthView. eventArgs; using System. windows; namespace MonthViewTest {/// <summary> // MainWindow. interaction logic of xaml // </summary> public partial class MainWindow: Window {public MainWindow () {InitializeComponent (); // The Event source TestMonthControl. dateTimeEvents = DiaryCache. diaries; // item interface factory TestMonthControl. dateTimeEventControlFactory = new DiaryBreifControlFactory (); TestMonthControl. dayBlankDoubleClicked + = AddNewDiary ;} /// <summary> /// double-click processing of the date control /// </summary> /// <param name = "e"> </param> private void AddNewDiary (dayEventArgs e) {var diary = new Diary {HappenTime = e. dayTime ,}; var wnd = new DiaryWindow {Diary = diary, Title = "add Diary", Owner = Application. current. mainWindow}; if (true = wnd. showDialog () {DiaryCache. diaries. add (wnd. diary );}}}}Principle
The code structure is as follows:
The core code is in DayControl. The page creation process is as follows: Use System. Globalization. Calendar to calculate the total number of days of the month, and then calculate the total number of weeks based on the number of weeks on the first day of the month. Add the week control of the corresponding week number to the month control, and then add the day control corresponding to the day of the month to the corresponding week control.
Disadvantage: The item list needs to be loaded to the memory at a time. You can modify the code and use the delegate to load it as needed. Controls do not support generics. If you do not need to use them in XAML, you can modify the code to make them support generics.
Code
For the code, see WPFMonthView.