WPF工具包日期选择器如何更改默认值“显示日历”

10

我正在使用最新的WPF工具包,具体来说是DatePicker。 一切正常,但是在没有提供值时,日期选择框中会出现默认的“SHOW CALENDAR”文本。

我想要在WPF中能够更改这个值。

有人告诉我下载源代码,添加一个新的依赖属性并重新编译成dll。那很棒,但如果发布新版本怎么办?

这就是为什么我想以这种方式对此控件进行模板化,以便我能够覆盖此默认字符串。 有任何想法吗?

谢谢!


2
我在Matt Hamilton的博客上找到了更好的解决方案(http://matthamilton.net/datepicker-watermark)。对我来说非常有效! - BrightShadow
4个回答

22

好的,我已经自己找到了解决方案。

<Style TargetType="{x:Type toolkit:DatePickerTextBox}">
    <Setter Property="Text" Value="Bitte wählen" />
</Style>
无论如何,你必须意识到事实上有一个名为Watermark的DependencyProperty应该代替Text进行设置。问题是,最新的MS版本(约在2009年6月左右)出于某种未知的原因将此属性设置为只读。这意味着,虽然会出现一次First-time异常(因为DatePicker试图解析字符串,他认为文本是日期),但通常你不会注意到它,这是我想出来的唯一hack。另一个可能性是直接编辑来自MS的源代码并覆盖SetWaterMark()方法+添加自己的Dependency Property(MyWaterMark或其他内容)。但那样你就不能使用提供的dll了。他们说它将在.NET 4发布时修复,让我们拭目以待。

这很好,但我们如何将其融入到现有的针对日期选择器的样式中呢? - David Brunelle
这似乎只在我的应用程序设计时起作用,一旦我启动,文本就会恢复到英文。 - Morten Nilsen

7
    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);
                }
            }
        }
    }

你需要使用相同的代码连接Load事件。

1
我必须承认,我喜欢你关于反射的例子。无论如何,你需要一个代码后台。 - theSpyCry

0

这个很好用,但是你还需要在自定义类中重写onrender方法。 在这个方法中,如果你设置了水印内容而不是属性,那么就没有必要重写OnSelectedDateChanged事件。 完整的代码在这里:

    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

您也可以这样做:

在表单中添加文本框和日期选择器,并进行以下设置:

在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"/>

在你的 window.xaml.cs 文件中:

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

结果看起来像这样:点击


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接