一些节日的更改背景的日历控件日期 [英] Change background of some holiday dates in Calendar control

查看:583
本文介绍了一些节日的更改背景的日历控件日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF中的桌面应用基本日历控件,我的要求是区分假期(日期这是从mysql数据库来)使用C#语言从正规的日期。我的代码是这样的:

I am using a basic calendar control in wpf desktop app and my requirement is to differentiate holidays (dates which are coming from mysql database) from regular dates by using c# language. My code is like :

<Calendar Name="MyCalendar"  Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" Grid.RowSpan="6"   Foreground="Aqua" SelectionMode="MultipleRange" SelectedDatesChanged="MyCalendar_SelectedDatesChanged" ></Calendar>



C#:

C# :

 private void MyCalendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            listboxSelectedDates.Items.Clear();
            foreach (DateTime dr in MyCalendar.SelectedDates)
            {
                listboxSelectedDates.Items.Add(dr.ToShortDateString());
            }//add selected dates in list

          MySqlConnection conn = new MySqlConnection("Server=localhost;database=newcompanydatabase;pwd=Admin@123;uid=root;");
            string q = "select _date from event_calendar;";
            MySqlCommand cmd = new MySqlCommand(q,conn);
            conn.Open();
            MySqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                if(MyCalendar.SelectedDate==rdr.GetDateTime(0))
                {
                    MessageBox.Show("******* holiday");                    

                }
            }//if selected date is holiday, show occasion.

        }



我怎样才能改回使用C#假期的颜色?

How can I change back color of holidays using c#?

推荐答案

我们可以创建一个 DataTrigger 的每一个假期。

We can create one DataTrigger for every holiday.

<Style x:Key="cdbKey" TargetType="CalendarDayButton">
    <Style.Triggers>
          <DataTrigger Binding="{Binding Date}" Value="12/07/2015">
              <Setter Property="Background" Value="Turquoise"/>
          </DataTrigger>
          ... more such DataTriggers for every holiday
    </Style.Triggers>
</Style>



但是,随着节日的日期都存储在数据库中,所以我们需要添加 DataTrigger 在循环中使用的代码。

But as holiday dates are stored in the database, so we need to add DataTrigger using code in a loop.


  1. 定义风格打靶 CalenderDayButton

<Style x:Key="cdbKey" TargetType="CalendarDayButton">
...                         
</Style>


  • 设置 CalendarDayButtonStyle 属性

    <日历... CalendarDayButtonStyle ={StaticResource的cdbKey}>

    获取从DB您的度假日期在适当的位置(窗户构造等)。

    Get your holiday dates from DB at proper place (window constructor etc).

    List<DateTime> holidaysDates = _getHolidayDatesFromDB();
    


  • 添加 DataTriggers 使用循环在第3步收到节日的日期列表,使用C#代码,并将其添加到风格上面定义的。

  • Add DataTriggers using a loop for holiday dates list received in Step 3, using C# code and add them to the Style defined above.

            Style s = (Style)this.Resources["cdbKey"];
    
            /* Loop through the Dates retrieved from DataBase*/
              DateTime holidayDate = DateTime.Parse("10/02/2015");
              DataTrigger dataTrigger = new DataTrigger() { Binding = new Binding("Date"), Value = holidayDate };
              dataTrigger.Setters.Add(new Setter(CalendarDayButton.BackgroundProperty, Brushes.SandyBrown));
              s.Triggers.Add(dataTrigger);
            /*End Loop*/
    

    我们已经使用绑定( 日期) DataTrigger 我们的绑定属性值,这是因为 CalenderDayButton 是有的DataContext 自动设置为的DateTime 值。

    We have used Binding("Date") as our Binding property value in DataTrigger, this is because CalenderDayButton is having DataContext set automatically to a DateTime value.

    我已经改变了背景 CalenderDayButton 来黄褐,你可以得到票友。

    I have changed Background of CalenderDayButton to SandyBrown, you can get fancier.

    这篇关于一些节日的更改背景的日历控件日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆