如何查找 WPF TextBox 的更改文本?
How to find WPF TextBox's changed text?
假设我们从 WPF TextBox
继承,然后尝试通过重写 OnTextChanged
.
来获取文本更改
然后我们会注意到何时发生变化,但我们唯一拥有的信息是:
- 此更改发生的偏移量
- 删除的长度
- 增加的长度
我们可以使用
来获取准确添加的文本吗?
Text.Substring(Change.Offset, Change.AddedLength)
在 OnTextChanged
?
文本更改发生在不同的条件下(例如用户输入、粘贴文本或在代码中设置 Text
属性)。 e.Changes
中是否存在冲突更改的可能性?
这种方式是信任方式吗?如果答案是否定的,是否有任何其他标准方法来获得准确的更改文本?
我用一个 TextBox 和一个 Label 做了一个简短的测试程序,我找不到你描述的问题。如果我像下面这样为 TextBox 实现 TextChanged 事件,即使我粘贴 something.The 也需要检查 null,因为如果 Textbox 具有初始值,Initilialize 方法将在创建标签之前触发更改事件。
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tbSource = (sender as TextBox);
if (lblOutput == null)
return;
lblOutput.Content = tbSource.Text;
}
对于文本框,当文本更改时发生此事件;对于 RichTextBox,当任何内容或格式更改(例如,图像、table 或背景颜色)时都会发生此事件。
- 我们可以使用...获得准确的添加文本吗?
是的,对于 TextBox,您将始终获得准确的文本。您可以使用 e.UndoAction 来检查文本的 addition/deletion。
阅读 documentation 此处。
- e.Changes 是否有可能发生冲突的更改?
不会有冲突的更改。
一般来说,以下情况总是成立的:
The changes that occur result in the document being in a valid state.
The collection is ordered consecutively, related to where the change occurred in the control. For example, a TextChange object
that represents a change at position 2 is before a TextChange
object that represents a change at position 10.
Two TextChange objects do not represent an overlapping area. The value of Offset plus the value of AddedLength of one TextChange
object is always less than or equal to the value of Offset of the
next TextChange object in the collection. Likewise, the value of
Offset plus the value of RemovedLength of one TextChange object is
always less than or equal to the value of Offset of the next
TextChange object in the collection.
The collection reflects whatever changes occurred, even if there seems to be no net change. In the preceding example, neither the
first or fourth change results in a net change, because each simply
removed and re-added the and symbols, respectively. But
the symbols were actually removed and added, so they are included in
the collection.
更多可以阅读here。
假设我们从 WPF TextBox
继承,然后尝试通过重写 OnTextChanged
.
然后我们会注意到何时发生变化,但我们唯一拥有的信息是:
- 此更改发生的偏移量
- 删除的长度
- 增加的长度
我们可以使用
来获取准确添加的文本吗?Text.Substring(Change.Offset, Change.AddedLength)
在 OnTextChanged
?
文本更改发生在不同的条件下(例如用户输入、粘贴文本或在代码中设置 Text
属性)。 e.Changes
中是否存在冲突更改的可能性?
这种方式是信任方式吗?如果答案是否定的,是否有任何其他标准方法来获得准确的更改文本?
我用一个 TextBox 和一个 Label 做了一个简短的测试程序,我找不到你描述的问题。如果我像下面这样为 TextBox 实现 TextChanged 事件,即使我粘贴 something.The 也需要检查 null,因为如果 Textbox 具有初始值,Initilialize 方法将在创建标签之前触发更改事件。
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tbSource = (sender as TextBox);
if (lblOutput == null)
return;
lblOutput.Content = tbSource.Text;
}
对于文本框,当文本更改时发生此事件;对于 RichTextBox,当任何内容或格式更改(例如,图像、table 或背景颜色)时都会发生此事件。
- 我们可以使用...获得准确的添加文本吗?
是的,对于 TextBox,您将始终获得准确的文本。您可以使用 e.UndoAction 来检查文本的 addition/deletion。 阅读 documentation 此处。
- e.Changes 是否有可能发生冲突的更改?
不会有冲突的更改。
一般来说,以下情况总是成立的:
The changes that occur result in the document being in a valid state. The collection is ordered consecutively, related to where the change occurred in the control. For example, a TextChange object that represents a change at position 2 is before a TextChange object that represents a change at position 10.
Two TextChange objects do not represent an overlapping area. The value of Offset plus the value of AddedLength of one TextChange
object is always less than or equal to the value of Offset of the
next TextChange object in the collection. Likewise, the value of
Offset plus the value of RemovedLength of one TextChange object is
always less than or equal to the value of Offset of the next
TextChange object in the collection.The collection reflects whatever changes occurred, even if there seems to be no net change. In the preceding example, neither the
first or fourth change results in a net change, because each simply
removed and re-added the and symbols, respectively. But
the symbols were actually removed and added, so they are included in
the collection.
更多可以阅读here。