WPF toolkit DatePicker change default value 'show calendar'

前端 未结 4 1446
时光取名叫无心
时光取名叫无心 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:37

    OK. I found a solution by myself.

    <Style TargetType="{x:Type toolkit:DatePickerTextBox}">
        <Setter Property="Text" Value="Bitte wählen" />
    </Style>
    

    Anyways, you have to be aware of the fact, that there is a DependencyProperty called Watermark which should be set in place of the Text.

    The problem is that with the latest MS release (about June 2009) they made this property readonly for some unknown reason. That means, this is the only hack I made up, although there occurs a First-time exception, because the DatePicker is trying to parse the string (he supposes the text to be a Date), but normally you won't notice it.

    Another possibility is to edit directly the source code from MS and override the SetWaterMark() method + add your own Dependency Property (MyWaterMark or something). But then you cannot use the provided dll. They said it will come fixed with the .NET 4 realese, let's see.

    0 讨论(0)
  • 2020-12-16 19:47
        void _datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_datePicker1.SelectedDate != null) return;
    
            FieldInfo fiTextBox = typeof(DatePicker)
                .GetField("_textBox", BindingFlags.Instance | BindingFlags.NonPublic);
    
            if (fiTextBox != null)
            {
                DatePickerTextBox dateTextBox =
                  (DatePickerTextBox)fiTextBox.GetValue(_datePicker1);
    
                if (dateTextBox != null)
                {
                    PropertyInfo piWatermark = dateTextBox.GetType()
                      .GetProperty("Watermark", BindingFlags.Instance | BindingFlags.NonPublic);
    
                    if (piWatermark != null)
                    {
                        piWatermark.SetValue(dateTextBox, "", null);
                    }
                }
            }
        }
    

    you'll need to hook up the Load event as well with the same code.

    0 讨论(0)
  • 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;
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-16 19:59

    You could also do this:

    Add a textbox and a datepicker to your form with the following settings:

    In your window.xaml:

    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="22" Margin="38,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <DatePicker x:Name="datePicker" HorizontalAlignment="Left" Margin="130,37,0,0" VerticalAlignment="Top" Width="28" BorderBrush="Transparent" SelectedDateChanged="datePicker_SelectedDateChanged"/>
    

    And in your window.xaml.cs:

    private void datePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            textBox.Text = datePicker.SelectedDate.Value.ToString("dd.MM.yyyy");
        }
    

    The result looks like this: klick

    0 讨论(0)
提交回复
热议问题