WPF-无法通过键盘输入日期?
我有一个WPF用户控件,它使用组合框来包含日期选择器。不幸的是,当我将日期选择器放入组合框时,我似乎失去了通过键盘输入日期的能力;只有日历和鼠标工作 有什么想法吗 另外,这里是我的XAML:WPF-无法通过键盘输入日期?,wpf,xaml,user-controls,Wpf,Xaml,User Controls,我有一个WPF用户控件,它使用组合框来包含日期选择器。不幸的是,当我将日期选择器放入组合框时,我似乎失去了通过键盘输入日期的能力;只有日历和鼠标工作 有什么想法吗 另外,这里是我的XAML: <ComboBox Name="cboCombo" IsReadOnly="True" IsEditable="True" Foreground="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Use
<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>
我会尝试mode=twoway,并确保在属性上设置了get,然后启动事件。我知道你想要这个活动,但是如果这个活动正在破坏它,那么至少你知道这个活动正在破坏它。您可以跟踪并查看是否已启动。我同意其他意见-为什么要这样做?我猜这与处理键盘事件的ComboBox控件有关,但是为什么您首先希望ComboBox中有一个日期选择器?如果您只想支持某些日期,我相信DatePicker已经可以支持这一点了——您不需要将它构建到组合框中。如果您试图支持不同类型的字段,那么这种组合并不是最好的方式,不仅仅是从编程的角度,而且从用户体验的角度来看。我建议研究其他想法,比如一组单选按钮与每个字段类型的控件配对,并且只启用与所选单选按钮对应的控件。它位于组合框中的原因是我想要日历的“弹出”功能,但我还需要其他选项,如选择时间和日期、选择今天或清除日期的按钮,以及显示所选日期是否为假日的标签。我当然愿意接受满足这些要求的其他建议。如果你想要所有这些功能,那么对这些单独的功能使用单独的控件。日期的日期选择器,时间的某种时间选择器(自己滚动或在线查看),将日期选择器设置为今天日期的按钮,iSoliday的复选框(或文本块)。。。试着把它放在一张单子上有点,嗯,古怪。不是真的。为什么我要在我的项目中创建200个控件,然后再创建200个方法来设置和重置值,而20个usercontrols就足够了?这不是拥有用户控件的全部意义吗?将所需的bahvior封装到一个包中。