use*_*614 3 c# wpf datepicker wpf-controls
我正在向DatePicker我的表单添加一个 WPF控件并且它工作正常。但我不希望用户能够在“选择日期”文本框中输入类型。我希望它是只读的,当他们单击文本框时,它只会打开日历。
我不确定属性中是否有此选项?我找不到任何东西...
<DatePicker HorizontalAlignment="Left" VerticalAlignment="Top">
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox">
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Text" Value=" "/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="IsEnabled" Value="False" />
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</DatePicker.Resources>
</DatePicker>
Run Code Online (Sandbox Code Playgroud)
将来,您可以使用 WPF 可视化工具查看顶级控件正在使用哪个子控件(在本例中为 DatePickerTextBox),然后像我在此处所做的那样,在资源部分将样式和/或模板应用于该类型。
马克·费尔德曼的答案是部分正确的,他没有回答如何单击文本框将打开日历弹出窗口!
您应该挂钩 的模板DatePicker并放大按钮的大小,以便它与文本框重叠:然后您就完成了,当您单击文本框时,按钮将获取事件,请参阅下面修改后的模板的一部分DatePicker:
<DatePickerTextBox Grid.Row="0" Grid.Column="0" x:Name="PART_TextBox" Focusable="{TemplateBinding Focusable}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
<Button Grid.Row="0" Grid.Column="0" x:Name="PART_Button" Foreground="{TemplateBinding Foreground}" Focusable="False" >
<Button.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border Background="Transparent"/>
<Button Grid.Column="1" HorizontalAlignment="Left" Foreground="{TemplateBinding Foreground}" Template="{StaticResource DropDownButtonTemplate}" VerticalAlignment="Top" Width="20" Margin="3,0,3,0" />
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
Run Code Online (Sandbox Code Playgroud)
DatePickerTextBox不要忘记添加Mark Feldman 的答案中描述的风格