2011-08-17 70 views
1

我有一個使用ComboBox包含DatePicker的WPF用戶控件。不幸的是,當我把DatePicker放入ComboBox時,我似乎失去了通過鍵盤輸入日期的能力;只有日曆和鼠標工作。WPF - 通過鍵盤輸入日期的能力喪失了嗎?

任何想法?

而且,這裏是我的XAML:

<ComboBox Name="cboCombo" IsReadOnly="True" IsEditable="True" Foreground="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=Foreground}" DropDownOpened="cboCombo_DropDownOpened" DropDownClosed="cboCombo_DropDownClosed"> 
      <ComboBox.Text> 
       <MultiBinding Converter="{StaticResource DateTimeTextConverter}" Mode="OneWay"> 
        <Binding RelativeSource="{RelativeSource FindAncestor,AncestorType={x:Type UserControl}}" Path="SelectedDateAndTime" Mode="OneWay" /> 
        <Binding RelativeSource="{RelativeSource FindAncestor,AncestorType={x:Type UserControl}}" Path="ShowTime" /> 
       </MultiBinding> 
      </ComboBox.Text> 
      <ComboBoxItem IsSelected="True"> 
       <ComboBoxItem.Template> 
        <ControlTemplate> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto" /> 
           <RowDefinition Height="Auto" /> 
           <RowDefinition Height="Auto" /> 
           <RowDefinition Height="Auto" /> 
          </Grid.RowDefinitions> 

          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 

          <DatePicker Grid.Row="0" Grid.Column="0" SelectedDate="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=SelectedDateAndTime}" SelectedDateChanged="BCDatePicker_SelectedDateChanged" /> 
          <controls:TimePicker Grid.Row="0" Grid.Column="1" MouseUp="TimePicker_MouseUp" Loaded="TimePicker_Loaded" Time="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=SelectedDateAndTime,Mode=OneWay}" Visibility="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=ShowTime, Converter={StaticResource VisibilityConverter}}" /> 

          <Button Grid.Row="1" Grid.Column="0" Content="Today" Click="Button_Click" /> 
          <Button Grid.Row="1" Grid.Column="1" Content="Clear" Click="Button_Click_1" /> 

          <Button Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Content="View Date Calculator" Click="Button_Click_2" Visibility="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=ShowDateCalculator,Mode=OneWay,Converter={StaticResource VisibilityConverter}}" /> 

          <Label Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Content="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=SelectedDateAndTime,Mode=OneWay,Converter={StaticResource HolidayDateConverter}}" Foreground="Red" /> 
         </Grid> 
        </ControlTemplate> 
       </ComboBoxItem.Template> 
      </ComboBoxItem> 
     </ComboBox> 
+3

我猜測它與處理KeyBoard事件的ComboBox控件有關,但爲什麼要在一個ComboBox中首次使用DatePicker? – Rachel

+0

如果您只想支持某些日期,我相信DatePicker已經可以支持這個日期了 - 您無需將其構建到組合框中。如果你試圖在那裏支持不同類型的字段,那麼這種組合不是*最好的方式來實現它,不僅僅是從編程的角度來看,而且從用戶體驗的角度來看。我建議調查其他想法,比如一組單選按鈕與每種字段類型的控件配對,並且只啓用與所選單選按鈕相對應的控件。 –

+0

它在組合框中的原因是我想要日曆的「彈出」功能,但我還需要其他選項,如選擇時間以及日期,用於選擇今天或清除日期的按鈕,以及標籤顯示所選日期是否爲假期。 我絕對願意接受其他符合這些要求的建議。 –

回答

0

我會嘗試模式=雙向,並確保有一大家子的屬性設置,並取下事件。我知道你想要這個活動,但如果這個活動正在打破它,那麼至少你知道這個活動正在打破它。而且你可以追蹤並查看是否設置了是否被解僱。我與其他評論 - 你爲什麼要這樣做?