WPF 工具包 DateTimePicker 使时间文本框不可编辑

WPF Toolkit DateTimePicker make Time Textbox un-editable

我正在尝试使日历的时间选择器成为只读文本框的一部分。因为默认情况下它允许用户输入 无效时间并抛出索引超出范围。我喜欢只允许从上下箭头选择时间,并禁止在该文本框中输入内容。

我试过使用 TimePickerVisibilityhidden 但它隐藏了时间选择器。

更新 1
我刚刚注意到您使用的是 DateTimePicker 而不是 TimePicker,
所以你需要以这种方式将 DateTimePicker 的 TimePicker 设置为不允许 AllowTextInput:

    <xctk:DateTimePicker HorizontalAlignment="Left" Margin="67,200,0,0" VerticalAlignment="Top" Width="228">
        <xctk:DateTimePicker.Resources>
            <Style TargetType="{x:Type xctk:TimePicker}">
                <Setter Property="AllowTextInput" Value="False"/>
            </Style>
        </xctk:DateTimePicker.Resources>
    </xctk:DateTimePicker>

更新2
如果你想禁用所有 TextInputs,这样用户就不能输入日期和时间,你可以这样做:

<xctk:DateTimePicker HorizontalAlignment="Left" Margin="67,200,0,0" VerticalAlignment="Top" Width="228">
    <xctk:DateTimePicker.Resources>
        <Style TargetType="{x:Type xctk:DateTimePicker}">
            <Setter Property="AllowTextInput" Value="False"/>
        </Style>
        <Style TargetType="{x:Type xctk:TimePicker}">
            <Setter Property="AllowTextInput" Value="False"/>
        </Style>
    </xctk:DateTimePicker.Resources>
</xctk:DateTimePicker>

如果您将使用标准的 DatePicker,请按以下方式完成。 对于标准 WPF DatePicker,您需要将 DatePickerTextBox 设置为 ReadOnly。你可以这样做:

<DatePicker HorizontalAlignment="Left" Margin="138,82,0,0" VerticalAlignment="Top">
    <DatePicker.Resources>
        <Style TargetType="DatePickerTextBox">
            <Setter Property="IsReadOnly" Value="True"/>
        </Style>
    </DatePicker.Resources>
</DatePicker>