WPF工具包DatePicker更改默认值“显示日历”

发布于 2024-07-26 17:32:17 字数 280 浏览 2 评论 0原文

我正在使用最新的 WPF 工具包,特别是 DatePicker。 一切正常,但当未提供任何值时,默认的“显示日历”文本将出现在 DatePickerTextBox 中。 我希望能够在 WPF 中更改此值。

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

这就是为什么我想以这种方式模板化这个控件,这样我就能够覆盖这个默认字符串。 知道该怎么做吗?

谢谢!

I'm using the latest WPF toolkit, specifically the DatePicker. Everything works fine, but when no value is provided, the default 'SHOW CALENDAR' text appears in the DatePickerTextBox.
I want to be able to change this value in WPF.

One told me to download the source, add a new Dependency property and recompile to dll. That's cool but what if new version is released?

That's why I'd like to template this control in that way, that I'll be able to override this default string. Any idea how to do that?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深爱成瘾 2024-08-02 17:32:17

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

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

无论如何,您必须意识到这一事实,即有一个名为 Watermark 的 DependencyProperty,应设置它来代替 Text。

问题是,在最新的 MS 版本(大约 2009 年 6 月)中,他们出于某种未知原因将此属性设为只读。 这意味着,这是我编造的唯一黑客,尽管出现了首次异常,因为 DatePicker 正在尝试解析字符串(他假设文本是日期),但通常您不会注意到它。

另一种可能性是直接编辑 MS 的源代码并覆盖SetWaterMark()方法+添加您自己的依赖属性(MyWaterMark 或其他)。 但是这样你就不能使用提供的dll。 他们说它将通过 .NET 4 realese 进行修复,让我们拭目以待。

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.

伤痕我心 2024-08-02 17:32:17
    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 事件。

    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.

旧伤慢歌 2024-08-02 17:32:17

这非常有效,但您还必须重写自定义类中的 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;
                }
            }
        }
    }

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;
                }
            }
        }
    }
北城孤痞 2024-08-02 17:32:17

您还可以执行以下操作:

使用以下设置将文本框和日期选择器添加到表单中:

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

结果如下所示: 点击

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文