我不要消息“ SELECT DATE”在DatePicker中 [英] I Don't want message "SELECT DATE" in DatePicker

查看:53
本文介绍了我不要消息“ SELECT DATE”在DatePicker中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在DatePicker的文本框中显示选择日期,但我想看到类似这样的内容 / / ____或其他文本。

I don't want display "Select Date" in textbox of DatePicker but I want see something like this //____ or another text.

这是我的资源

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="ABC" TargetType="DatePicker">
    <Setter Property="Foreground" Value="#FF333333" />
    <Setter Property="IsTodayHighlighted" Value="True" />
    <Setter Property="SelectedDateFormat" Value="Short" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="DisplayDateStart" Value=" 01/01/1990" />
    <Setter Property="DisplayDateEnd" Value="12/31/2090" />        
    <Setter Property="Padding" Value="2"/>
    <Setter Property="FontFamily" Value="Verdana" />
    <Setter Property="FontSize" Value="14" />

    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="HorizontalAlignment" Value ="Center" />
    <Setter Property="VerticalAlignment" Value= "Center" />

    <Setter Property = "Text" Value="{x:Null}" />
    <Setter Property = "SelectedDate" Value="{x:Null}" />

    <Style.Triggers>
        <Trigger Property ="IsMouseOver" Value="True">
            <Setter Property= "Background" Value="Coral"/>
        </Trigger>

        <Trigger Property="IsFocused" Value="True">
            <Setter Property= "Background" Value="LemonChiffon"/>
        </Trigger>

    </Style.Triggers>
</Style>

推荐答案

您可以为 DatePickerTextBox

修改默认模板。要更改的是 PART_Watermark ContentControl。 DatePicker的代码设置了此控件的Content属性,因此我们不能仅更改Content。而是重写此控件的ControlTemplate并将其设置为带有所需文本的TextBlock。

The thing to change is the 'PART_Watermark' ContentControl. The code for the DatePicker sets the Content property of this control, so we can't just change the Content. Instead override the ControlTemplate for this control and just make it a TextBlock with the text you want.

<ContentControl x:Name="PART_Watermark"
                    Opacity="0"
                    Focusable="False"
                    IsHitTestVisible="False"
                    Padding="2">
    <ContentControl.Template>
        <ControlTemplate>
            <TextBlock Text="//____"/>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

这是整个事情:

<Style TargetType="{x:Type DatePickerTextBox}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DatePickerTextBox">
                <Grid>
                    <Grid.Resources>
                        <SolidColorBrush x:Key="WatermarkBrush" Color="#FFAAAAAA"/>
                    </Grid.Resources>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0" />
                                <VisualTransition To="MouseOver" GeneratedDuration="0:0:0.1" />
                            </VisualStateGroup.Transitions>
                            <VisualState Name="Normal" />
                            <VisualState Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" To="#FF99C1E2" Duration="0"/>
                                    <ColorAnimation Storyboard.TargetName="watermark_decorator" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" To="#FF99C1E2" Duration="0"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup Name="WatermarkStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0" />
                            </VisualStateGroup.Transitions>
                            <VisualState Name="Unwatermarked" />
                            <VisualState Name="Watermarked">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                    <DoubleAnimation Storyboard.TargetName="PART_Watermark" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup Name="FocusStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0" />
                            </VisualStateGroup.Transitions>
                            <VisualState Name="Unfocused" />
                            <VisualState Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>


                    <Border x:Name="Border" 
                            Background="{TemplateBinding Background}" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}"
                            CornerRadius="1" 
                            Opacity="1">
                        <Grid x:Name="WatermarkContent"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <Border x:Name="ContentElement" BorderThickness="1">
                                <Border.BorderBrush>
                                    <SolidColorBrush Color="#FFFFFFFF"/>
                                </Border.BorderBrush>
                            </Border>
                            <Border x:Name="watermark_decorator" BorderThickness="1">
                                <Border.BorderBrush>
                                    <SolidColorBrush Color="#FFFFFFFF"/>
                                </Border.BorderBrush>
                                <ContentControl x:Name="PART_Watermark"
                                                    Opacity="0"
                                                    Focusable="False"
                                                    IsHitTestVisible="False"
                                                    Padding="2">
                                    <ContentControl.Template>
                                        <ControlTemplate>
                                            <TextBlock Text="//____"/>
                                        </ControlTemplate>
                                    </ContentControl.Template>
                                </ContentControl>
                            </Border>
                            <ScrollViewer x:Name="PART_ContentHost" 
                                          Margin="0"
                                          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
                            <Border x:Name="FocusVisual" BorderBrush="#FF45D6FA" CornerRadius="1" Opacity="0" IsHitTestVisible="False"/>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这篇关于我不要消息“ SELECT DATE”在DatePicker中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆