Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Persian Diary in WPF

0.00/5 (No votes)
6 Jan 2010 5  
With Persian Diary, you can navigate in Persian Calendar and store your memorandums
Persian Calendar in WPF

Introduction

With this software, you can navigate the Persian Calendar and you can add your memorandum for each day.

Using the Code

I've created this project with .NET Framework 3.5 SP1, so you need to install it to use this project.
I've also used a SQL database(mdf) to store the data, so at least you have to install SQL Express 2008.

Creating the UI

The best way for creating a software UI like this is to use a Stack.

Then we should use UniformGrids and Grids in the Stack to create the UI.
For instance, the XAML code for 1 in the above figure is:

<StackPanel x:Name="stackPanel">
    <StackPanel.BitmapEffect>
        <BlurBitmapEffect Radius="0"/>
    </StackPanel.BitmapEffect>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Center" 
        x:Name="header" MinHeight="70">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.135*"/>
            <ColumnDefinition Width="0.102*"/>
            <ColumnDefinition Width="0.494*"/>
            <ColumnDefinition Width="0.14*"/>
            <ColumnDefinition Width="0.129*"/>
        </Grid.ColumnDefinitions>
        <TextBlock FlowDirection="RightToLeft" Language="fa-ir" 
            FontSize="36" x:Name="TextBlockThisMonth" HorizontalAlignment="Center" 
            Grid.Column="2" VerticalAlignment="Center" Margin="0,0,0,0" 
            FontFamily="Arial">تاریخ</TextBlock>

        <Button x:Name="previousMonth" Content=" ماه قبل &gt; " 
            Grid.Column="3" VerticalAlignment="Center" Click="previousMonth_Click" 
            HorizontalAlignment="Center" />
        <Button x:Name="nextMonth" Content=" &lt; ماه بعد " Grid.Column="1" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            Click="nextMonth_Click" />

        <Button x:Name="previousYear" Content=" سال قبل &gt;&gt; " 
            Grid.Column="4" HorizontalAlignment="Center" VerticalAlignment="Center" 
            Click="previousYear_Click" />
        <Button x:Name="nextYear" Content=" &lt;&lt; سال بعد " 
            HorizontalAlignment="Center"  Click="nextYear_Click" 
            VerticalAlignment="Center"  />

    </Grid>
    <UniformGrid Columns="7" Rows="1" x:Name="dayNames" MinHeight="30">
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyleForHolydays}"/>
            <TextBlock HorizontalAlignment="Center" 
            VerticalAlignment="Center"><Run FlowDirection="RightToLeft" 
            Language="fa-ir" Text="جمعه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="پنج شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="چهار شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="سه شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="دو شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}"/>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="یک شنبه"/></TextBlock>
        </Grid>
        <Grid>
            <Rectangle Style="{DynamicResource RectangleStyle1}" />
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" 
            FontSize="14"><Run FlowDirection="RightToLeft" Language="fa-ir" 
            Text="شنبه"/></TextBlock>
        </Grid>
    </UniformGrid>
    <!-- *********************************************************** -->
    (The XAML of 2)
     .
    .
    .
</StackPanel>

The XAML code for 2 in the above figure is:

<UniformGrid Columns="7" Rows="1" Margin="0,10,0,0" 
	x:Name="calendarRow01" MinHeight="80">
    <Grid x:Name="GridJome0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="RectangleJome0" 
			Style="{DynamicResource RectangleStyleForHolydays}" />
        <TextBlock x:Name="TextBlockJome0Miladi" 
        	Style="{DynamicResource TextBlockStyle6}">08 Dec</TextBlock>
        <TextBlock x:Name="TextBlockJome0Hijri" 
        	Style="{DynamicResource TextBlockStyle10}">۰۱ محرم</TextBlock>
        <TextBlock x:Name="TextBlockJome0" 
        	Style="{DynamicResource TextBlockStyle1}">جمعه</TextBlock>
    </Grid>
    <Grid x:Name="Grid5Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle5Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock5Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock5Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock5Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">پنج شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid4Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle4Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock4Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock4Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock4Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">چهار شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid3Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle3Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock3Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock3Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock3Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">سه شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid2Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle2Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock2Shanbe0Miladi" Text="08 Dec" 
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlock2Shanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
        <TextBlock x:Name="TextBlock2Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">دو شنبه</TextBlock>
    </Grid>
    <Grid x:Name="Grid1Shanbe0" Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="Rectangle1Shanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlock1Shanbe0Miladi" 
        	Style="{DynamicResource TextBlockStyle6}" Text="08 Dec" Margin="0,0,10,5"/>
        <TextBlock x:Name="TextBlock1Shanbe0Hijri" 
        	Style="{DynamicResource TextBlockStyle10}" Text="۰۱ محرم" />
        <TextBlock x:Name="TextBlock1Shanbe0" 
        	Style="{DynamicResource TextBlockStyle1}">یک شنبه</TextBlock>
    </Grid>
    <Grid x:Name="GridShanbe0"  Style="{DynamicResource GridStyle}">
        <Rectangle x:Name="RectangleShanbe0" 
        	Style="{DynamicResource RectangleStyle2}" />
        <TextBlock x:Name="TextBlockShanbe0" 
        	Style="{DynamicResource TextBlockStyle1}" >شنبه</TextBlock>
        <TextBlock x:Name="TextBlockShanbe0Miladi" Text="08 Dec"  
        	Style="{DynamicResource TextBlockStyle6}"/>
        <TextBlock x:Name="TextBlockShanbe0Hijri" Text="۰۱ محرم" 
        	Style="{DynamicResource TextBlockStyle10}"/>
    </Grid>
</UniformGrid>

The XAML code for 3 in the above figure is:
<Grid x:Name="Grid5Shanbe0" Style="{DynamicResource GridStyle}">
    <Rectangle x:Name="Rectangle5Shanbe0" 
    	Style="{DynamicResource RectangleStyle2}" />
    <TextBlock x:Name="TextBlock5Shanbe0Miladi" Text="08 Dec" 
    	Style="{DynamicResource TextBlockStyle6}"/>
    <TextBlock x:Name="TextBlock5Shanbe0Hijri" Text="۰۱ محرم" 
    	Style="{DynamicResource TextBlockStyle10}"/>
    <TextBlock x:Name="TextBlock5Shanbe0" 
    	Style="{DynamicResource TextBlockStyle1}">پنج شنبه</TextBlock>
</Grid>

The XAML code for 4 in the above figure is:

<Grid x:Name="footer" MinHeight="70">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.121*"/>
        <ColumnDefinition Width="0.132*"/>
        <ColumnDefinition Width="0.07*"/>
        <ColumnDefinition Width="0.119*"/>
        <ColumnDefinition Width="0.3*"/>
        <ColumnDefinition Width="0.084*"/>
        <ColumnDefinition Width="0.064*"/>
        <ColumnDefinition Width="0.11*"/>
    </Grid.ColumnDefinitions>

    <Button x:Name="goToToday" Content="  پرش به امروز  " 
    	Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" 
        Click="goToToday_Click"  />

    <WindowsFormsHost Grid.Column="0" FontSize="13" VerticalAlignment="Center" 
    	HorizontalAlignment="Center" Width="70" Height = "25">
        <wf:MaskedTextBox Text="سال"  x:Name="textBoxYear" Mask="0000"/>
    </WindowsFormsHost>

    <ComboBox Grid.Column="1" FontSize="13" HorizontalAlignment="Center" 
    	VerticalAlignment="Center" IsSynchronizedWithCurrentItem="True" 
        	Width="85" x:Name="comboBoxMonths">
        <ComboBoxItem Content="فروردین" IsSelected="True"/>
        <ComboBoxItem Content="اردیبهشت"/>
        <ComboBoxItem Content="خرداد"/>

        <ComboBoxItem Content="تیر"/>
        <ComboBoxItem Content="مرداد"/>
        <ComboBoxItem Content="شهریور"/>

        <ComboBoxItem Content="مهر"/>
        <ComboBoxItem Content="آبان"/>
        <ComboBoxItem Content="آذر"/>

        <ComboBoxItem Content="دی"/>
        <ComboBoxItem Content="بهمن"/>
        <ComboBoxItem Content="اسفند"/>
    </ComboBox>

    <Button x:Name="goToDate" Content="  برو  " Grid.Column="2" 
    	VerticalAlignment="Center" HorizontalAlignment="Center" 
        Click="goToDate_Click" />
    <TextBlock Text="تنظیم ماه قمری" Grid.Column="7" HorizontalAlignment="Center" 
    	VerticalAlignment="Center"/>
    <TextBox x:Name="TextBoxHijriAdjustment" Text="" Grid.Column="6" 
    	HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="30" />
    <Button x:Name="ButttonHijriAdjustment" Content="  تنظیم  " 
    	Grid.Column="5" HorizontalAlignment="Center" VerticalAlignment="Center" 
        Click="ButttonHijriAdjustment_Click"  />
        
</Grid>

I also created a couple of Storyboards to show and hide the below panel.
This panel updates the database with your input data.

The XAML of Storyboards is given below:

<Storyboard x:Key="showEventGrid">
    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="EventGrid" 
    	Storyboard.TargetProperty="(UIElement.Visibility)">
        <DiscreteObjectKeyFrame KeyTime="00:00:00" 
        Value="{x:Static Visibility.Visible}"/>
        <DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" 
        Value="{x:Static Visibility.Visible}"/>
    </ObjectAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
        Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>
    </DoubleAnimationUsingKeyFrames>
    <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" Storyboard.TargetProperty
        	="(FrameworkElement.Margin)">
        <SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,0,0,0"/>
        <SplineThicknessKeyFrame KeyTime="00:00:00.5000000" Value="0,0,0,0"/>
    </ThicknessAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="stackPanel" 
        Storyboard.TargetProperty="(UIElement.BitmapEffect)
        	.(BlurBitmapEffect.Radius)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="6"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="hideEventGrid">
    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
        Storyboard.TargetProperty="(UIElement.Visibility)">
        <DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" 
        Value="{x:Static Visibility.Hidden}"/>
    </ObjectAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
		Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="stackPanel" 
        Storyboard.TargetProperty="(UIElement.BitmapEffect).(BlurBitmapEffect.Radius)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
    <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" 
    	Storyboard.TargetName="EventGrid" 
        Storyboard.TargetProperty="(FrameworkElement.Margin)">
        <SplineThicknessKeyFrame KeyTime="00:00:00.5000000" Value="0,0,0,0"/>
    </ThicknessAnimationUsingKeyFrames>
</Storyboard>

The C# Code

In Persian Calendar, we have 12 months that I have shown in the following code snippet:

//Season { MonthName(nomber of days in the month) ...}
Spring { Farvardin(31), OrdiBehesht(31), Khordad(31) }
Summer { Tir(31), Mordad(31), Shahrivar(31) }
Autumn { Mehr(30), Aban(30), Azar(30) }
Winter { Day(30), Bahman(30), Esfand(30 or 29) }

Fortunately there is a class in .NET for creating Persian Calendar and I've used it.

PersianCalendar persianCalendar = new PersianCalendar();

The main method in this project to create a Persian month is calculateMonth.

/// <summary>
/// The main method to show the calendar
/// This method shows `thisMonth` in `thisYear`
/// </summary>
void calculateMonth(int thisYear, int thisMonth)
{
    try
    {
        yearForNavigating = thisYear;
        monthForNavigating = thisMonth;

        DateTime tempDateTime = persianCalendar.ToDateTime(yearForNavigating, 
        	monthForNavigating, 15, 01, 01, 01, 01);

        int thisDay = 1;
        TextBlockThisMonth.Text = "";
        TextBlockThisMonth.Text =
            monthForNavigating.convertToPersianMonth() + " " +
            yearForNavigating.convertToPersianNumber();

        //Different between first place of calendar and first place of this month
        //اختلاف بین خانه شروع ماه و اولین خانه تقویم            
        string DayOfWeek = persianCalendar.GetDayOfWeek(persianCalendar.
        	ToDateTime(thisYear, thisMonth, 01, 01, 01, 01, 01)).ToString();
        int span = calculatePersianSpan(DayOfWeek.convertToPersianDay());

        decreasePersianDay(ref thisYear, ref thisMonth, ref thisDay, span);

        string persianDate;//حاوی تاریخ روزهای شمسی Contains the date of Persian
        string miladiDate;//حاوی تاریخ روزهای میلادی Contains the date of Christian
        string hijriDate;//حاوی تاریخ روزهای قمری Contains the date of Hijri

        string tooltip_context = "";//Contains the text of tooltip

        ////////////////////////////////////

        for (int i = 0; i < 6 * 7; i++)
        {
            tempDateTime = persianCalendar.ToDateTime(thisYear, thisMonth, 
            	thisDay, 01, 01, 01, 01);

            miladiDate = tempDateTime.Day.ToString() + " " + 
            	englishMonthName(tempDateTime.Month) 
            	+ " " + tempDateTime.Year.ToString();
            hijriDate = hijriCalendar.GetDayOfMonth
		(tempDateTime).convertToPersianNumber() 
                + " " + hijriCalendar.GetMonth
			(tempDateTime).convertToHigriMonth() + " " 
                + hijriCalendar.GetYear(tempDateTime).convertToPersianNumber();
            persianDate = thisDay.convertToPersianNumber();

            DayOfWeek = persianCalendar.GetDayOfWeek(tempDateTime).ToString();

            if (thisMonth == monthForNavigating)
            {
                tooltip_context = "";
                if (thisDay == currentDay && thisMonth == currentMonth && 
                	thisYear == currentYear)//بررسی تاریخ امروز
                {
                    tooltip_context = GetTextOfMemo
			(thisYear, thisMonth, thisDay, "Shamsi");
                    if (tooltip_context == "")
                        tooltip_context = GetTextOfMemo(tempDateTime.Year, 
                        	tempDateTime.Month, 
                        	tempDateTime.Day, "Miladi");
                    if (tooltip_context == "")
                        tooltip_context = GetTextOfMemo
				(hijriCalendar.GetYear(tempDateTime), 
                        	hijriCalendar.GetMonth(tempDateTime), 
                            	hijriCalendar.GetDayOfMonth(tempDateTime), "Hijri");

                    if (DayOfWeek.convertToPersianDay() == "جمعه")//بررسی جمعه بودن روز
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyleForHolydays_CurrentDay", "TextBlockStyle3", 
                            "TextBlockStyle12", "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyleToday", 
			"TextBlockStyle5", "TextBlockStyle10", 
                            "TextBlockStyle6", tooltip_context);
                }
                else if (searchShamsiTable(thisYear, thisMonth, thisDay))
                {
                    tooltip_context = GetTextOfMemo
			(thisYear, thisMonth, thisDay, "Shamsi");
                    if (isHoliday(thisYear, thisMonth, thisDay, "Shamsi"))
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle5", "TextBlockStyle3", "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle4", "TextBlockStyle1", "TextBlockStyle10", 
                        	"TextBlockStyle6", tooltip_context);
                }
                else if (searchHijriTable(hijriCalendar.GetYear(tempDateTime), 
                	hijriCalendar.GetMonth(tempDateTime), 
                    hijriCalendar.GetDayOfMonth(tempDateTime)))
                {
                    tooltip_context = GetTextOfMemo
					(hijriCalendar.GetYear(tempDateTime), 
                    	hijriCalendar.GetMonth(tempDateTime), hijriCalendar.
                        GetDayOfMonth(tempDateTime), "Hijri");
                    if (isHoliday(thisYear, thisMonth, thisDay, "Hijri"))
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle5", "TextBlockStyle3", "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle4", "TextBlockStyle5", "TextBlockStyle10", 
                            "TextBlockStyle6", tooltip_context);
                }
                else if (searchMiladiTable(tempDateTime.Year, tempDateTime.Month, 
                	tempDateTime.Day))
                {
                    tooltip_context = GetTextOfMemo
			(tempDateTime.Year, tempDateTime.Month, 
                    	tempDateTime.Day, "Miladi");
                    if (isHoliday(thisYear, thisMonth, thisDay, "Miladi"))
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle5", "TextBlockStyle3", "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle4", "TextBlockStyle5", "TextBlockStyle10", 
                        	"TextBlockStyle6", tooltip_context);
                }
                else
                {
                    if (DayOfWeek.convertToPersianDay() == 
					"جمعه")//بررسی جمعه بودن روز
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyleForHolydays", "TextBlockStyle3", 
                            "TextBlockStyle12", 
                            "TextBlockStyle8", tooltip_context);
                    else
                        changeProperties(i, persianDate, hijriDate, miladiDate, 
                        	"RectangleStyle2", "TextBlockStyle5", "TextBlockStyle10", 
                            "TextBlockStyle6", tooltip_context);
                }
            }
            else
            {
                if (DayOfWeek.convertToPersianDay() == "جمعه")//بررسی جمعه بودن روز
                    changeProperties(i, persianDate, hijriDate, miladiDate, 
                    	"RectangleStyleForOtherHolydays", "TextBlockStyle4", 
                        "TextBlockStyle13", "TextBlockStyle9", tooltip_context);
                else
                    changeProperties(i, persianDate, hijriDate, miladiDate, 
                    	"RectangleStyle3", 
                    	"TextBlockStyle2", "TextBlockStyle11", "TextBlockStyle7", 
                        tooltip_context);
            }

            increasePersianDay(ref thisYear, ref thisMonth, ref thisDay, 1);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, 
        	MessageBoxImage.Error);
    }
}

Then I've used LINQ to update the database.
First I've added a LINQ to SQL class:

Then I designed DataClasses1.dbml:

Then we can update the database easily with a LINQ query.
For instance, I wrote this method to search shamsi table.

/// <summary>
/// Searches the Database shamsi table
/// </summary>
/// <returns>If the input data is found in the table return true, else 
/// return false</returns>
bool searchShamsiTable(int year, int month, int day)
{
    bool result = false;
    try
    {
        int? id = null;
        id = (from q in db.shamsis
              where (q.year == year && q.day == day && q.month == month) 
              	|| (q.anniversary == true && q.month == month && q.day == day)
              select q.id).First();
        if (id != null) result = true;
    }
    catch { result = false; }
    return result;
} 

Further reading about LINQ to SQL can be done here.
Other methods are simple and easy to learn and I've explained them with some comments.

Point of Interest

The point of interest in this project (for me) was, using a WinForm control in WPF.
I've used a WinForm Masked TextBox for input year.

For using a WinForm Masked TextBox in a WPF App: I added these DLLs to my references:

  • WindowsFormsIntegration.dll
  • System.Windows.Forms.dll 

Then I added the following line within Window tag in the XAML source:

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

At last, I added the MaskedTextBox control to my WPF App with WindowsFormsHost tag.
<WindowsFormsHost Grid.Column="0" FontSize="13" 
	VerticalAlignment="Center" HorizontalAlignment="Center" 
    Width="70" Height = "25">
	<wf:MaskedTextBox x:Name="textBoxYear" Mask="0000"/>
</WindowsFormsHost>

Further reading about WindowsFormHost can be done here.

History

  • 12th February, 2009: First post
  • 6 th January , 2010 : Database was changed to a XML file and some Bugs were resolved.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here