WPF Datepicker to disable user input

前端 未结 6 2196
离开以前
离开以前 2021-02-19 08:44

I have a datepicker, but it is allowing me to enter any text. I want to disable user from entering text. User should be allowed to select date from the calendar.



        
相关标签:
6条回答
  • 2021-02-19 09:00

    This one worked for me (global style for all datepickers):

    <Style TargetType="{x:Type DatePickerTextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <TextBox IsReadOnly="True" x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd.MM.yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    Be aware: it also sets a custom string format.

    0 讨论(0)
  • 2021-02-19 09:02

    Add the property

    Focusable = "False"

    to your datepicker. This should not allow user to enter any string in datepicker textbox

    <DatePickerTextBox Grid.Column="1" Grid.Row="3" Margin="2" Name="SelectedDate"   IsReadOnly="True" Focusable="False"></DatePickerTextBox>
    
    0 讨论(0)
  • 2021-02-19 09:02

    No need to set any thing to datepicker, when you out focus datepicker will remove text automatically, only date was remain in textbox.

    0 讨论(0)
  • 2021-02-19 09:08

    Just set the IsEnabled to false and that should solve your problem

    0 讨论(0)
  • 2021-02-19 09:10

    Set the IsHitTestVisible and Focusable properties to false.

    0 讨论(0)
  • 2021-02-19 09:12

    seems like there is no property on DatePicker to make the TextBox read only.

    Try the below style setting

            <DatePicker x:Name="MyDatePicker">
                <DatePicker.Resources>
                    <Style TargetType="DatePickerTextBox">
                        <Setter Property="IsReadOnly" Value="True"/>
                    </Style>
                </DatePicker.Resources>
            </DatePicker>
    
    0 讨论(0)
提交回复
热议问题