简体   繁体   中英

Silverlight/WPF Calendar how to get visible dates

I want to get the visible dates in my calendar, for example for the following image I want to get June 28th of 2015 to August 8th of 2015

在此处输入图片说明

All I get in the event DisplayDateChanged is

AddedDate = {01/07/2015 00:00:00} (July 1st)

RemovedDate = {25/06/2015 00:00:00} (June 25th)

At first I thought that DisplayDateStart and DisplayDateEnd would give me this information, but I realized that these properties are not readonly, instead I set them for other purposes such as the date range that will be displayed.

So any workaround or way to calculate or get the result I want?

I got my answer through some questions and doing some changes in my calendar template.

I clicked Edit Additional Templates / Edit CalendarDayButtonStyle , then I added the following:

<Setter Property="Tag" Value="{Binding Path=Date}" />

private void myCal_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            var grid = FindVisualChildByName<Grid>(myCal, "MonthView");
            DateTime? dtBegin = null;
            DateTime? dtEnd = null;

            if (grid != null && grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Any())
            {
                foreach (var button in grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Cast<System.Windows.Controls.Primitives.CalendarDayButton>())
                {
                    if (dtBegin == null)
                        dtBegin = Convert.ToDateTime(button.Tag);
                    else
                        dtEnd = Convert.ToDateTime(button.Tag);
                }
            }
        }

        public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                string controlName = child.GetValue(Control.NameProperty) as string;
                if (controlName == name)
                {
                    return child as T;
                }
                else
                {
                    T result = FindVisualChildByName<T>(child, name);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }

Detecting when a day is clicked on the Silverlight Calendar control

https://pwnedcode.wordpress.com/2009/04/01/find-a-control-in-a-wpfsilverlight-visual-tree-by-name/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM