【问题标题】:WPF toolkit DatePicker change default value 'show calendar'WPF 工具包 DatePicker 更改默认值“显示日历”
【发布时间】:2010-11-09 06:40:22
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

标签: datepicker wpftoolkit


【解决方案1】:

好的。我自己找到了解决办法。

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

无论如何,您必须了解以下事实,即应该设置一个名为 Watermark 的 DependencyProperty 来代替 Text。

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

另一种可能性是直接从 MS 编辑源代码并 覆盖 SetWaterMark() 方法 + 添加您自己的依赖属性(MyWaterMark 或其他东西)。但是你不能使用提供的dll。他们说它会在.NET 4 realese中修复,让我们看看。

【讨论】:

  • 这很好,但是我们如何才能将其合并到针对 Datepicker 的现有样式中???
  • 这似乎只适用于我的应用程序的设计时间,一旦我启动,文本就会恢复为英文
【解决方案2】:
    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 事件。

【讨论】:

  • 好吧,我不得不承认,我喜欢你的反射示例......无论如何......你需要一个代码
【解决方案3】:

这很好用,但您还必须在自定义类中覆盖 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;
                }
            }
        }
    }

【讨论】:

    【解决方案4】:

    你也可以这样做:

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

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

    结果如下所示:klick

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多