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

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

c# - WPF 工具包 DateTimePicker 使时间文本框不可编辑-LMLPHP

最佳答案

更新 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>

关于c# - WPF 工具包 DateTimePicker 使时间文本框不可编辑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43865735/

10-11 15:59
查看更多