Search code examples
wpfxamldatepickertextboxcontroltemplate

WPF DatePicker textbox white border not editable


I am editing the control template for the DatePicker but there is an issue with the TextBox style. There is a white border around the text which IS NOT the actual TextBox border. With my current style, I have the TextBox inside a Border with the TextBox Background set to transparent and the BorderThickness set to 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>

This is what the DatePicker looks like with this Style:

raw

If I add a Border to the TextBox, it appears around the white Border:

bordered

If anyone has any idea on how to remove this extra Border it would be much appreciated!


Solution

  • You can create a new Style for the DatePickerTextBox:

    XAML Style

    This is just a copy of the original Style and changed the Visibility of the two Border to 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

    Preview

    If you create a new Style you can also include your "Khaki" Border with the CornerRadius.


    Thanks @OfficeAddinDev

    This answer removes the border by setting the "watermark_decorator" Border. Visibility = Hidden. Simply change this to Visible and change BorderThickness = 0 instead to show "Select a date" without the menacing white border.