【问题标题】:Disable DatePickerTextBox content highlight禁用 DatePickerTextBox 内容突出显示
【发布时间】:2016-07-26 16:21:54
【问题描述】:

在使用 DatePicker 时遇到了一些问题。有点卡住了,可以使用一些帮助/重新审视问题。 目标是为 DatePicker 创建自定义样式,主要区别在于:

  • 保持简单,尽可能少地使用添加;
  • 日期选择器文本框角半径;
  • 日历按钮进入 DatePickerTextBox;
  • 禁用文本框内容突出显示框。

    问题:
    我正在使用来自资源字典的自定义样式和键。 到目前为止,我通过了第一个目标,并且发现第二个目标没有问题。但最后一个,男孩,似乎很艰难。唯一可行的解​​决方案是设置属性 Focusable="False",但这不是一个选项,因为它会切断手动输入日期的能力。

    风格如下:

    <Style x:Key="DatePicker" TargetType="{x:Type DatePicker}">
        <Setter Property="Foreground" Value="{StaticResource DarkGrayBrush}" />
        <Setter Property="Focusable" Value="True" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="VerticalContentAlignment" Value="Top" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="IsTodayHighlighted" Value="True" />
        <Setter Property="SelectedDateFormat" Value="Short" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />        
        <!--Set CalendarStyle to DatePickerCalendarStyle.-->
        <Setter Property="CalendarStyle"
          Value="{DynamicResource DatePickerCalendarStyle}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DatePicker}">
                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}"
                            CornerRadius="0" 
                            Background="Transparent">                        
                        <Grid x:Name="PART_Root"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="150" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Button x:Name="PART_Button"
                                    Grid.Column="1"
                                    Foreground="{TemplateBinding Foreground}"
                                    Focusable="True"
                                    HorizontalAlignment="Left"
                                    Margin="1,0,1,0"
                                    Grid.Row="0"
                                    VerticalAlignment="Center" Style="{StaticResource IconButtonStyle}">
                                <Image Source="/img/calendar.png" Height="20" />
                            </Button>
                            <Border BorderBrush="{StaticResource BlueBrush}"
                                    BorderThickness="2"
                                    CornerRadius="0"
                                    Padding="0"
                                    Width="150"
                                    Height="23"
                                    Background="{StaticResource WhiteBrush}">
                                <DatePickerTextBox x:Name="PART_TextBox"                                               
                                                   Grid.Column="0"
                                                   Foreground="{TemplateBinding Foreground}"
                                                   Focusable="{TemplateBinding Focusable}"
                                                   FocusVisualStyle="{x:Null}"
                                                   HorizontalContentAlignment="Left"
                                                   Grid.Row="0"                                                   
                                                   SelectionBrush="{x:Null}"
                                                   VerticalContentAlignment="Center" 
                                                   BorderThickness="0"
                                                   Background="Transparent"
                                                   Width="150">
    
                                </DatePickerTextBox>
                            </Border>
                            <Grid x:Name="PART_DisabledVisual"
                                  Grid.ColumnSpan="2"
                                  Grid.Column="0"
                                  IsHitTestVisible="False"
                                  Opacity="0"
                                  Grid.Row="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Rectangle Grid.Column="0"
                                        Fill="#A5FFFFFF"
                                        RadiusY="1"
                                        Grid.Row="0"
                                        RadiusX="1" />
                                <Rectangle Grid.Column="1"
                                         Fill="#A5FFFFFF"
                                         Height="18"
                                         Margin="3,0,3,0"
                                         RadiusY="1"
                                         Grid.Row="0"
                                         RadiusX="1"
                                         Width="19" />
                                <Popup x:Name="PART_Popup"
                                         AllowsTransparency="True"
                                         Placement="Bottom"
                                         PlacementTarget="{Binding ElementName=PART_TextBox}"
                                         StaysOpen="False" />
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    任何帮助将不胜感激。 谢谢。

    更新:
    这是我的solution的链接:

  • 【问题讨论】:

      标签: wpf xaml datepicker


      【解决方案1】:

      您所指的高亮帧是由 DatePicker 控件中的内置视觉状态(MouseOver)引起的,请尝试以下步骤:

      1. 为您的项目添加一个日期选择器控件。

      2. 右键单击日期选择器并选择“编辑模板\编辑副本...”。

      这会将您带到模板设计器。

      1. 文本框实际上是 datepicker 控件中的另一个控件,您现在需要更改文本框的模板,寻找一个名为“Part_Texbox”的控件,右键单击它并选择“Edit Template \ Edit a复制...”。

      2. 您需要更改“鼠标悬停”视觉状态的值,请查看此图片:https://postimg.org/image/ndqhew9g1/

      【讨论】:

      • 谢谢你的回复,我的朋友。其实我已经想通了,如何解决这个问题。您的回答完美地描述了如何使用 Blend(我没有使用)来解决它。我将使用手动编码的解决方案更新我的帖子。再次感谢您。
      猜你喜欢
      • 2020-09-18
      • 2014-12-17
      • 2015-07-11
      • 2014-11-12
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      相关资源
      最近更新 更多