ygo*_*goe 4 wpf textbox overwrite
当用户在WPF TextBox中按Insert键时,控件在插入和覆盖模式之间切换.通常,这是通过使用不同的光标(线对块)可视化的,但这不是这里的情况.由于用户完全没有办法知道覆盖模式是活动的,我只想完全禁用它.当用户按下Insert键时(或者可能有意或无意地激活该模式),TextBox应该保持插入模式.
我可以添加一些按键事件处理程序并忽略所有这些事件,按下没有修饰符的Insert键.那就够了吗?你知道一个更好的选择吗?我的视图中有很多TextBox控件,我不想在任何地方添加事件处理程序......
您可以制作AttachedProperty并使用ChrisF建议的方法,这样就可以在TextBoxes您的应用程序中添加到您想要的内容中
XAML:
<TextBox Name="textbox1" local:Extensions.DisableInsert="True" />
Run Code Online (Sandbox Code Playgroud)
AttachedProperty:
public static class Extensions
{
public static bool GetDisableInsert(DependencyObject obj)
{
return (bool)obj.GetValue(DisableInsertProperty);
}
public static void SetDisableInsert(DependencyObject obj, bool value)
{
obj.SetValue(DisableInsertProperty, value);
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisableInsertProperty =
DependencyProperty.RegisterAttached("DisableInsert", typeof(bool), typeof(Extensions), new PropertyMetadata(false, OnDisableInsertChanged));
private static void OnDisableInsertChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox && e != null)
{
if ((bool)e.NewValue)
{
(d as TextBox).PreviewKeyDown += TextBox_PreviewKeyDown;
}
else
{
(d as TextBox).PreviewKeyDown -= TextBox_PreviewKeyDown;
}
}
}
static void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Insert && e.KeyboardDevice.Modifiers == ModifierKeys.None)
{
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1520 次 |
| 最近记录: |