C# WPF文本框覆盖
在WPF中,是否有一个易于覆盖文本框中文本的方法 谢谢 托尼 编辑:我想我没有弄清楚。对不起 我有一个文本框,允许用户键入6个字符。如果用户键入6个字符,然后出于任何原因,将光标放在6个字符的中间或起始位置,并开始键入,那么我希望他们键入什么来覆盖字符。基本上就像Word中的覆盖模式一样C# WPF文本框覆盖,c#,wpf,C#,Wpf,在WPF中,是否有一个易于覆盖文本框中文本的方法 谢谢 托尼 编辑:我想我没有弄清楚。对不起 我有一个文本框,允许用户键入6个字符。如果用户键入6个字符,然后出于任何原因,将光标放在6个字符的中间或起始位置,并开始键入,那么我希望他们键入什么来覆盖字符。基本上就像Word中的覆盖模式一样 再次感谢。假设您的意思是选择一些文本,然后允许用户在该文本上键入: //select the third character textBox.Select(2, 1); 在Reflector中查看它,这似乎是
再次感谢。假设您的意思是选择一些文本,然后允许用户在该文本上键入:
//select the third character
textBox.Select(2, 1);
在Reflector中查看它,这似乎是由boolean TextBoxBase.TextEditor.\u OvertypeMode内部属性控制的。你可以通过思考来理解它:
// fetch TextEditor from myTextBox
PropertyInfo textEditorProperty = typeof(TextBox).GetProperty("TextEditor", BindingFlags.NonPublic | BindingFlags.Instance);
object textEditor = textEditorProperty.GetValue(myTextBox, null);
// set _OvertypeMode on the TextEditor
PropertyInfo overtypeModeProperty = textEditor.GetType().GetProperty("_OvertypeMode", BindingFlags.NonPublic | BindingFlags.Instance);
overtypeModeProperty.SetValue(textEditor, true, null);
我会避免反思。
最干净的解决方案如下:
EditingCommands.ToggleInsert.Executenull,myTextBox
嘿嘿,,
我知道这个问题非常古老,但我一直在寻找通过MVVM模式归档覆盖行为的解决方案。所以我写了下面的DependencyProperty,希望它能帮助别人
public class ElementBehavior
{
#region ForceOverride
///<summary>
/// DependencyProperty
///</summary>
public static readonly DependencyProperty ForceOverrideProperty = DependencyProperty.RegisterAttached("ForceOverride", typeof(bool), typeof(ElementBehavior), new PropertyMetadata(false, (s, e) =>
{
if (s is TextBoxBase t)
{
if ((bool)e.NewValue)
{
t.PreviewKeyDown += OnForceOverride;
}
else
{
t.PreviewKeyDown -= OnForceOverride;
}
}
}));
///<summary>
/// Get
///</summary>
///<param name="target"><see cref="DependencyObject"/></param>
///<returns><see cref="bool"/></returns>
public static bool GetForceOverride(DependencyObject target)
{
return (bool)target.GetValue(ForceOverrideProperty);
}
///<summary>
/// Set
///</summary>
///<param name="target"><see cref="DependencyObject"/></param>
///<param name="value"><see cref="bool"/></param>
public static void SetForceOverride(DependencyObject target, bool value)
{
target.SetValue(ForceOverrideProperty, value);
}
private static void OnForceOverride(object sender, KeyEventArgs e)
{
Key[] BAD_KEYS = new Key[] { Key.Back, Key.Delete };
Key[] WRK_KEYS = new Key[] { Key.Left, Key.Up, Key.Right, Key.Down, Key.Enter };
if (BAD_KEYS.Contains(e.Key))
{
e.Handled = true;
}
else if (!WRK_KEYS.Contains(e.Key))
{
if (sender is RichTextBox r)
{
if (!string.IsNullOrEmpty(new TextRange(r.Document.ContentStart, r.Document.ContentEnd).Text))
{
r.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input, (Action)delegate
{
TextPointer tp = r.CaretPosition.GetNextInsertionPosition(LogicalDirection.Forward);
if (tp != null && tp != r.Document.ContentEnd)
{
r.Selection.Select(r.CaretPosition, tp);
}
});
}
}
else if (sender is TextBox t)
{
if (!string.IsNullOrEmpty(t.Text))
{
t.Select(t.CaretIndex, 1);
}
}
}
}
#endregion
}
用法:
<Style TargetType="{x:Type TextBox}">
<Setter Property="ElementBehavior.ForceOverride" Value="True"/>
</Style>
这不适用于编程应用的文本。用户输入,当然。但您不能选择文本,然后通过代码写入文本框;它不会覆盖那里的内容。