2017-05-09 35 views
3

我正在嘗試使日曆的時間選擇器部分具有隻讀文本框。因爲默認情況下,它允許用戶鍵入 無效時間並引發索引超出範圍。我喜歡只允許時間選擇器向下箭頭,並禁用在該文本框中輸入。WPF工具包DateTimePicker使時間文本框不可編輯

我試過使用TimePickerVisibilityhidden,但它隱藏了時間選擇器。

enter image description here

+0

你可以處理事件點擊該特定的部分,什麼也不做......? – Noctis

回答

2

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

UPDATE2
如果要禁用所有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> 
+0

更新2就像一個魅力。 – mehwish

相關問題