WPF DatePicker只显示年和月 修改:可以只体现年

2.有关操练的实质

锤炼是或不是真的能减轻肥胖程度?对于平时越发繁忙的人群有更迅捷的磨练格局吗?什么是最大的平常化杀手?那部《关于磨炼的天柱山真面目》是BBC地平线类别的一期科学普及纪录片。片中,主持人迈克尔Mosley通过亲身实践,希望能找到降低血糖和内脏脂肪率,进而增强人体机能的磨砺方式。在她参预了几项有趣的尝试后,也意识众多有关磨练的惊人真相。

BBC纪录片磨炼的实质 – 高清在线观察 – 腾讯摄像

显示效果

4.糖脂之争

到底那种物质对我们的不荒谬尤其害?糖,如故脂肪?纪录片中的两名骨干是同卵双胞胎,分别食用了多少个月高糖食物(大致无脂肪)和三个月的高脂肪食物(大概没糖分),实验结果令人震惊。吃货必须要看……

[BBC地平线:糖脂大战].Sugar.V.Fat.2015-高清阅览-腾讯录像

图片 1图片 2图片 3

1.瘦身十律

大家每天都在身边、电视机、网路上被各样减轻肥胖程度、塑形的经营销售音讯轰炸着,然则到底如何方法确实可行呢?这么些名为《瘦身十律》的BBC纪录片,医疗记者MichaelMosley亲自调查科学瘦身的新星成功,揭穿十种最简便的减轻肥胖程度格局。他的发现恐怕会彻底改变你对节制饮食、健康和减肥的眼光。

BBC精选摄像:关于减轻肥胖程度你应该知道的十件事-高清观察-腾讯摄像

public class DatePickerDateFormat
    {
        public static readonly DependencyProperty DateFormatProperty =
DependencyProperty.RegisterAttached("DateFormat", typeof(string), typeof(DatePickerDateFormat),
                                    new PropertyMetadata(OnDateFormatChanged));

        public static string GetDateFormat(DependencyObject dobj)
        {
            return (string)dobj.GetValue(DateFormatProperty);
        }

        public static void SetDateFormat(DependencyObject dobj, string value)
        {
            dobj.SetValue(DateFormatProperty, value);
        }

        private static void OnDateFormatChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            var datePicker = (DatePicker)dobj;

            Application.Current.Dispatcher.BeginInvoke(
                DispatcherPriority.Loaded, new Action<DatePicker>(ApplyDateFormat), datePicker);
        }

        private static void ApplyDateFormat(DatePicker datePicker)
        {
            var binding = new Binding("SelectedDate")
            {
                RelativeSource = new RelativeSource { AncestorType = typeof(DatePicker) },
                Converter = new DatePickerDateTimeConverter(),
                ConverterParameter = new Tuple<DatePicker, string>(datePicker, GetDateFormat(datePicker))
            };
            var textBox = GetTemplateTextBox(datePicker);
            textBox.SetBinding(TextBox.TextProperty, binding);

            textBox.PreviewKeyDown -= TextBoxOnPreviewKeyDown;
            textBox.PreviewKeyDown += TextBoxOnPreviewKeyDown;

            datePicker.CalendarOpened -= DatePickerOnCalendarOpened;
            datePicker.CalendarOpened += DatePickerOnCalendarOpened;
        }

        private static TextBox GetTemplateTextBox(Control control)
        {
            control.ApplyTemplate();
            return (TextBox)control.Template.FindName("PART_TextBox", control);
        }

        private static void TextBoxOnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Return)
                return;

            /* DatePicker subscribes to its TextBox's KeyDown event to set its SelectedDate if Key.Return was
             * pressed. When this happens its text will be the result of its internal date parsing until it
             * loses focus or another date is selected. A workaround is to stop the KeyDown event bubbling up
             * and handling setting the DatePicker.SelectedDate. */

            e.Handled = true;

            var textBox = (TextBox)sender;
            var datePicker = (DatePicker)textBox.TemplatedParent;
            var dateStr = textBox.Text;
            var formatStr = GetDateFormat(datePicker);
            datePicker.SelectedDate = DatePickerDateTimeConverter.StringToDateTime(datePicker, formatStr, dateStr);
        }

        private static void DatePickerOnCalendarOpened(object sender, RoutedEventArgs e)
        {
            /* When DatePicker's TextBox is not focused and its Calendar is opened by clicking its calendar button
             * its text will be the result of its internal date parsing until its TextBox is focused and another
             * date is selected. A workaround is to set this string when it is opened. */

            var datePicker = (DatePicker)sender;
            var textBox = GetTemplateTextBox(datePicker);
            var formatStr = GetDateFormat(datePicker);
            textBox.Text = DatePickerDateTimeConverter.DateTimeToString(formatStr, datePicker.SelectedDate);
        }

        private class DatePickerDateTimeConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var formatStr = ((Tuple<DatePicker, string>)parameter).Item2;
                var selectedDate = (DateTime?)value;
                return DateTimeToString(formatStr, selectedDate);
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var tupleParam = ((Tuple<DatePicker, string>)parameter);
                var dateStr = (string)value;
                return StringToDateTime(tupleParam.Item1, tupleParam.Item2, dateStr);
            }

            public static string DateTimeToString(string formatStr, DateTime? selectedDate)
            {
                return selectedDate.HasValue ? selectedDate.Value.ToString(formatStr) : null;
            }

            public static DateTime? StringToDateTime(DatePicker datePicker, string formatStr, string dateStr)
            {
                DateTime date;
                var canParse = DateTime.TryParseExact(dateStr, formatStr, CultureInfo.CurrentCulture,
                                                      DateTimeStyles.None, out date);

                if (!canParse)
                    canParse = DateTime.TryParse(dateStr, CultureInfo.CurrentCulture, DateTimeStyles.None, out date);

                return canParse ? date : datePicker.SelectedDate;
            }
        }
    }

5.濒死病胖子的减轻肥胖程度之旅

那是一部分外鼓舞人心的励志纪录片。片子由编剧乔·Claus自编自己制片人自己扮演,亲自说法,讲述她的平常化减轻肥胖程度之旅:六十天前,100磅赘肉、多量类固醇和患有一种使人衰弱的免疫疾病,都使乔的1只脚踏进了坟墓。乔看上去的确是八方受敌了,而守旧的减轻肥胖程度药物也不可能长时间信赖。于是,乔做出了一个主要的操纵:他起来戒掉垃圾食物,发誓每一日只喝新鲜的榨汁饮料,并且开端她全新的正常活着方式。在加州圣巴巴拉分校州,乔遇见了一个平等过度肥胖的卡车司机Phil·斯台普斯。Phil已经是1个人重达429磅的严重肥胖病人,而且她的中枢也起先负荷不断他病态的体重,于是也参与乔初叶了减轻肥胖程度旅程。

——未经同意禁止转发。

2015年8月4日 记:

3.节制饮食与长寿

胖墩墩大概是当代人健康的最大敌人,节制饮食、间歇性断食等饮食方法能给人类带来健康与长寿呢?迈克尔Mosley大伯再一次出镜,肉体力行地涉足考试,并以多少个不难的事例来证实方便节制饮食有利于身一帆风顺康。

BBC纪录片展播区 – 高清在线观望 – 腾讯摄像

只呈现时间的类(二〇一四-12-20改动,能够只呈现年)

图片 4图片 5

总的调用方法 

2016年12月20日

<DatePicker conver:DatePickerCalendar.IsMonthYear="True" conver:DatePickerDateFormat.DateFormat="yyyy-MM" />

从图上就能看到左右差别,右侧的是添加附加属性的,点击以后直接显示月,后侧没用的则显获得日,纵然都格式化为了yyyy-MM,不过Calendar假如彰显到日的话,用户体验不佳。

 ========================================================================================================================

调用格局

明日在类型中发觉七个题材,正是运用小编上一篇帖子中的三句话进行DatePicker时间的格式化展现,不过出于用的是Thread,所以是在线程里开始展览格式化的,由此,影响到了同事做的任何模块,因为大多数都以格式化为yyyy-MM-dd,不过由于本人的3个页面是格式化为了yyyy-MM,而其间的3个同事并从未在她的构造函数里采用三句格式化他的DatePicker,从而导致他任何的时日计算出现难点,因而在担保原有效力不变的意况下,需求显示yyyy-MM,就要求用到原帖子中的另三个类。

public class DatePickerCalendar
    {

        public static bool GetIsYear(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsYearProperty);
        }

        public static void SetIsYear(DependencyObject obj, bool value)
        {
            obj.SetValue(IsYearProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsYear.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsYearProperty =
            DependencyProperty.RegisterAttached("IsYear", typeof(bool), typeof(DatePickerCalendar), new PropertyMetadata(false, new PropertyChangedCallback(OnIsMonthYearChanged)));



        public static readonly DependencyProperty IsMonthYearProperty =
        DependencyProperty.RegisterAttached("IsMonthYear", typeof(bool), typeof(DatePickerCalendar),
                                            new PropertyMetadata(OnIsMonthYearChanged));

        public static bool GetIsMonthYear(DependencyObject dobj)
        {
            return (bool)dobj.GetValue(IsMonthYearProperty);
        }

        public static void SetIsMonthYear(DependencyObject dobj, bool value)
        {
            dobj.SetValue(IsMonthYearProperty, value);
        }

        private static void OnIsMonthYearChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            var datePicker = (DatePicker)dobj;

            Application.Current.Dispatcher
                .BeginInvoke(DispatcherPriority.Loaded,
                             new Action<DatePicker, DependencyPropertyChangedEventArgs>(SetCalendarEventHandlers),
                             datePicker, e);
        }

        private static void SetCalendarEventHandlers(DatePicker datePicker, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == e.OldValue)
                return;

            if ((bool)e.NewValue)
            {
                datePicker.CalendarOpened += DatePickerOnCalendarOpened;
                datePicker.CalendarClosed += DatePickerOnCalendarClosed;
            }
            else
            {
                datePicker.CalendarOpened -= DatePickerOnCalendarOpened;
                datePicker.CalendarClosed -= DatePickerOnCalendarClosed;
            }
        }

        private static void DatePickerOnCalendarOpened(object sender, RoutedEventArgs routedEventArgs)
        {
            var calendar = GetDatePickerCalendar(sender);
            if (GetIsYear(sender as DatePicker))
            {
                calendar.DisplayMode = CalendarMode.Decade;
            }
            else
            {
                calendar.DisplayMode = CalendarMode.Year;
            }

            calendar.DisplayModeChanged += CalendarOnDisplayModeChanged;
        }

        private static void DatePickerOnCalendarClosed(object sender, RoutedEventArgs routedEventArgs)
        {
            var datePicker = (DatePicker)sender;
            var calendar = GetDatePickerCalendar(sender);
            datePicker.SelectedDate = calendar.SelectedDate;

            calendar.DisplayModeChanged -= CalendarOnDisplayModeChanged;
        }

        private static void CalendarOnDisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
        {
            var calendar = (Calendar)sender;
            var datePicker = GetCalendarsDatePicker(calendar);

            bool mode = (GetIsYear(datePicker) && calendar.DisplayMode != CalendarMode.Year) || (GetIsMonthYear(datePicker) && calendar.DisplayMode != CalendarMode.Month);

            if (mode)
                return;

            calendar.SelectedDate = GetSelectedCalendarDate(calendar.DisplayDate);

            datePicker.IsDropDownOpen = false;
        }

        private static Calendar GetDatePickerCalendar(object sender)
        {
            var datePicker = (DatePicker)sender;
            var popup = (Popup)datePicker.Template.FindName("PART_Popup", datePicker);
            return ((Calendar)popup.Child);
        }

        private static DatePicker GetCalendarsDatePicker(FrameworkElement child)
        {
            var parent = (FrameworkElement)child.Parent;
            if (parent.Name == "PART_Root")
                return (DatePicker)parent.TemplatedParent;
            return GetCalendarsDatePicker(parent);
        }

        private static DateTime? GetSelectedCalendarDate(DateTime? selectedDate)
        {
            if (!selectedDate.HasValue)
                return null;
            return new DateTime(selectedDate.Value.Year, selectedDate.Value.Month, 1);
        }
    }

初稿是用了八个类,当中三个是为着让DatePicker下的Calendar只突显时间,不出示日,另五个类是为了让DatePicker格式化为yyyy-MM格式,然而从文章中得以旁观,有人提议了,用格式化类举行格式化时,DatePicker控件会闪动一下,当然不影响使用。倘使不想接纳小说中涉及的类举行格式化,也足以用作者上一篇小说中的方法开始展览格式化,不汇合世闪动的景色。

========================================================================================================================

DatePickerDateFormat

DatePickerDateFormat

DatePickerCalendar

修改原来的代码,扩展IsYear附加属性,用法和IsMonthYear一样,设置为True则只展现年份

图片 6图片 7

前不久的类型,查询时只要求年和月,不须要日,因此须求对原有的DatePicker进行修改,查询了网上的内容,最后从一篇帖子里看到了增加附加属性的法门,地址是http://stackoverflow.com/questions/1798513/wpf-toolkit-datepicker-month-year-only

<DatePicker Width="200" Height="30" local:DatePickerCalendar.IsMonthYear="True"/>