MaterialDesignThemes Wpf TimePicker为不可键入

g0czyy6m  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(97)
MaterialDesignThemes.Wpf.TimePicker timePicker = new MaterialDesignThemes.Wpf.TimePicker();
            timePicker.Name = name.Replace("/", "000").Trim();
            timePicker.Tag = name;
            timePicker.Is24Hours = true;
            timePicker.Style = (Style)Application.Current.Resources["MaterialDesignTimePicker"];
            timePicker.PreviewTextInput += NumberValidationTextBox;
            timePicker.Margin = new Thickness(10);
            timePicker.BorderThickness = new Thickness(1);
            timePicker.HorizontalAlignment = HorizontalAlignment.Stretch;
            timePicker.Foreground = Brushes.Black;

我正在创建一个从材料设计主题的时间选择器。现在的时间选择器有选择从时钟时间或可以类型的文本,我想使时间选择器不可键入。只有用户可以从时钟中选择时间。

bweufnob

bweufnob1#

若要使MaterialDesignThemes.Wpf.TimePicker不可键入,可以禁用控件的键盘输入。可以通过处理PreviewKeyDown事件并将事件参数的Handled属性设置为true以防止处理任何按键来实现此目的。下面是你可以修改你的代码的方法:

MaterialDesignThemes.Wpf.TimePicker timePicker = new MaterialDesignThemes.Wpf.TimePicker();
timePicker.Name = name.Replace("/", "000").Trim();
timePicker.Tag = name;
timePicker.Is24Hours = true;
timePicker.Style = (Style)Application.Current.Resources["MaterialDesignTimePicker"];
timePicker.PreviewTextInput += NumberValidationTextBox;
timePicker.PreviewKeyDown += (sender, e) =>
{
    // Prevent any keyboard input
    e.Handled = true;
};
timePicker.Margin = new Thickness(10);
timePicker.BorderThickness = new Thickness(1);
timePicker.HorizontalAlignment = HorizontalAlignment.Stretch;
timePicker.Foreground = Brushes.Black;

通过添加PreviewKeyDown事件处理程序并设置e.Handled = true;在它里面,你有效地阻止了TimePicker中的任何键盘输入,使其不可键入,同时仍然允许用户从时钟中选择时间。

相关问题