WPF DatePicker 文本框白色边框不可编辑

标签 wpf xaml datepicker textbox controltemplate

我正在编辑 DatePicker 的控件模板,但 TextBox 样式存在问题。文本周围有一个白色边框,这不是实际的 TextBox 边框。 按照我目前的风格,我将 TextBox 放在 Border 中,将 TextBox Background 设置为透明,将 BorderThickness 设置为 0。

<Border Grid.Column="0" 
        Height="25"
        Background="DarkKhaki"
        CornerRadius="5"
        Grid.ColumnSpan="1">

<DatePickerTextBox x:Name="PART_TextBox" 
                   Background="Transparent"
                   Grid.Column="0" 
                   BorderThickness="0"
                   Focusable="{TemplateBinding Focusable}" 
                   HorizontalContentAlignment="Stretch" 
                   Grid.Row="0" 
                   Grid.ColumnSpan="1" 
                   Margin="1.143,0,1.056,0"/>
                            </Border>

这是 DatePicker 使用此样式的样子:

raw

如果我将 Border 添加到 TextBox,它会出现在白色 Border 周围:

bordered

如果有人知道如何删除这个额外的Border,我们将不胜感激!

最佳答案

您可以为 DatePickerTextBox 创建一个新的 Style:

XAML 样式

这只是复制了原来的Style,将两个BorderVisibility改成了Hidden .

<Style x:Key="DatePickerTextBoxStyle1" 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="{x:Type DatePickerTextBox}">
                        <Grid>
                            <Grid.Resources>
                                <SolidColorBrush x:Key="WatermarkBrush" Color="#FFAAAAAA"/>
                            </Grid.Resources>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0"/>
                                        <VisualTransition GeneratedDuration="0:0:0.1" To="MouseOver"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimation Duration="0" To="#FF99C1E2" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="ContentElement"/>
                                            <ColorAnimation Duration="0" To="#FF99C1E2" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="watermark_decorator"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="WatermarkStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Unwatermarked"/>
                                    <VisualState x:Name="Watermarked">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentElement"/>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_Watermark"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Unfocused"/>
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisual"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="1" Opacity="1" Padding="{TemplateBinding Padding}">
                                <Grid x:Name="WatermarkContent" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                    <Border x:Name="ContentElement" BorderBrush="#FFFFFFFF" BorderThickness="1" Visibility="Hidden"/>
                                    <Border x:Name="watermark_decorator" BorderBrush="#FFFFFFFF" BorderThickness="1" Visibility="Hidden">
                                        <ContentControl x:Name="PART_Watermark" Focusable="False" IsHitTestVisible="False" Opacity="0" Padding="2"/>
                                    </Border>
                                    <ScrollViewer x:Name="PART_ContentHost" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                    <Border x:Name="FocusVisual" BorderBrush="#FF45D6FA" CornerRadius="1" IsHitTestVisible="False" Opacity="0"/>
                                </Grid>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

XAML

    <Border
        Background="DarkKhaki" 
        VerticalAlignment="Center" 
        HorizontalAlignment="Center"
        CornerRadius="5">
        <DatePickerTextBox 
            x:Name="datePickerTextBox" 
            Height="25" 
            Width="120" 
            TextWrapping="Wrap" 
            Text="DatePickerTextBox" 
            Background="Transparent"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"                 
            Style="{DynamicResource DatePickerTextBoxStyle1}"/>
    </Border>

预览

Preview

如果您创建一个新的Style,您还可以将“Khaki”Border 包含在CornerRadius 中。


感谢@OfficeAddinDev

此答案通过设置“watermark_decorator”边框来移除边框。 可见性 = 隐藏。只需将其更改为 Visible 并更改 BorderThickness = 0 即可显示“选择日期”而没有令人生畏的白色边框。

关于WPF DatePicker 文本框白色边框不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44020869/

相关文章:

c# - 在 XAML 中将 ListBox 绑定(bind)到列表(集合)

c# - 仅在属性存在时绑定(bind)到属性

wpf - "Prefix ' .... ' does not map to a namespace",设计师错误?

javascript - 选择日期后日期选择器自动关闭不起作用

javascript - Bootstrap Datepicker 在 Ruby-on-Rails 中的位置和行为

c# - Wpf 绑定(bind)桥

wpf - 在 ItemsSource 中设置 Binding Mode 就足够了吗?

c# - WPF Multibinding 未按预期更新源;带有 'Select All' 的复选框

c# - 如何为 WPF DataGrid 制作条纹颜色?

javascript - 使用 Pickadate.js 时出现问题 - 显示效果不佳