WPF Datepicker禁用用户输入 [英] WPF Datepicker to disable user input

查看:69
本文介绍了WPF Datepicker禁用用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期选择器,但是它允许我输入任何文本.我想禁止用户输入文本.应该允许用户从日历中选择日期.

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>

推荐答案

似乎在DatePicker上没有使TextBox只读的属性.

seems like there is no property on DatePicker to make the TextBox read only.

尝试以下样式设置

        <DatePicker x:Name="MyDatePicker">
            <DatePicker.Resources>
                <Style TargetType="DatePickerTextBox">
                    <Setter Property="IsReadOnly" Value="True"/>
                </Style>
            </DatePicker.Resources>
        </DatePicker>

这篇关于WPF Datepicker禁用用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆