WPF toolkit DatePicker change default value 'show calendar'

前端 未结 4 1461
时光取名叫无心
时光取名叫无心 2020-12-16 19:05

I\'m using the latest WPF toolkit, specifically the DatePicker. Everything works fine, but when no value is provided, the default \'SHOW CALEND

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 19:55

    this works great but also you'll have to override onrender method in custom class. In this method if you set watermark content and not the property there is no need to override OnSelectedDateChanged event. Complete code is here:

        public string Watermark { get; set; }
    
        protected override void OnSelectedDateChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectedDateChanged(e);
            //SetWatermark();
        }
    
        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            SetWatermark();
        }
    
        private void SetWatermark()
        {
            FieldInfo fiTextBox = typeof(DatePicker).GetField("_textBox", BindingFlags.Instance | BindingFlags.NonPublic);
            if (fiTextBox != null)
            {
                DatePickerTextBox dateTextBox = (DatePickerTextBox)fiTextBox.GetValue(this);
                if (dateTextBox != null)
                {
                    if (string.IsNullOrWhiteSpace(this.Watermark))
                    {
                        this.Watermark = "Custom select a date";
                    }
    
                    //PropertyInfo piWatermark = typeof(DatePickerTextBox).GetProperty("Watermark", BindingFlags.Instance | BindingFlags.NonPublic);
                    //if (piWatermark != null)
                    //{
                    //    piWatermark.SetValue(dateTextBox, this.Watermark, null);
                    //}
    
                    var partWatermark = dateTextBox.Template.FindName("PART_Watermark", dateTextBox) as ContentControl;
                    if (partWatermark != null)
                    {
                        partWatermark.Foreground = new SolidColorBrush(Colors.Gray);
                        partWatermark.Content = this.Watermark;
                    }
                }
            }
        }
    

提交回复
热议问题