WPF Datepicker 禁用用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16740451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datepicker to disable user input
提问by user2418423
I have a datepicker, but it is allowing me to enter any text. I want to disable user from entering text. User should be allowed to select date from the calendar.
我有一个日期选择器,但它允许我输入任何文本。我想禁止用户输入文本。应允许用户从日历中选择日期。
<Window x:Class="MyTestForDatePicker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<!--<Grid>-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<DatePicker Grid.Column="0" Grid.Row="0" Margin="2" Name="MySelectedDate" VerticalContentAlignment="Center" ></DatePicker>
<DatePickerTextBox Grid.Column="1" Grid.Row="3" Margin="2" Name="SelectedDate" IsReadOnly="True"></DatePickerTextBox>
<Button Grid.Column="1" Grid.Row="1" Margin="100,102,203,154" VerticalContentAlignment="Center" Name="Ok" Click="Ok_Click_1"/>
</Grid>
回答by krishnaaditya
seems like there is no property on DatePicker to make the TextBox read only.
似乎 DatePicker 上没有使 TextBox 只读的属性。
Try the below style setting
试试下面的样式设置
<DatePicker x:Name="MyDatePicker">
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox">
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DatePicker.Resources>
</DatePicker>
回答by nandu
Add the property
添加属性
Focusable = "False"
Focusable = "False"
to your datepicker. This should not allow user to enter any string in datepicker textbox
到您的日期选择器。这不应允许用户在日期选择器文本框中输入任何字符串
<DatePickerTextBox Grid.Column="1" Grid.Row="3" Margin="2" Name="SelectedDate" IsReadOnly="True" Focusable="False"></DatePickerTextBox>
回答by Igor
Set the IsHitTestVisible
and Focusable
properties to false
.
将IsHitTestVisible
和Focusable
属性设置为false
。
回答by Indregaard
This one worked for me (global style for all datepickers):
这个对我有用(所有日期选择器的全局样式):
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox IsReadOnly="True" x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd.MM.yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Be aware: it also sets a custom string format.
请注意:它还设置了自定义字符串格式。
回答by Muzafar Hussain
No need to set any thing to datepicker, when you out focus datepicker will remove text automatically, only date was remain in textbox.
不需要为 datepicker 设置任何东西,当你离开焦点 datepicker 将自动删除文本,只有日期保留在文本框中。
回答by Kudakwashe Mafutah
Just set the IsEnabled to false and that should solve your problem
只需将 IsEnabled 设置为 false 即可解决您的问题