请参见以下示例:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1"
SizeToContent="WidthAndHeight">
<Window.DataContext>
<src:CodeName/>
</Window.DataContext>
<TextBox Text="{Binding Code, UpdateSourceTrigger=PropertyChanged}" />
</Window>
Imports System.ComponentModel
Public Class CodeName
Implements INotifyPropertyChanged
Private m_Code As String
Public Property Code() As String
Get
Return m_Code
End Get
Set(ByVal value As String)
If Not String.IsNullOrWhiteSpace(value) Then value = "_" & value
m_Code = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Code"))
End Set
End Property
Public Event PropertyChanged(ByVal sender As Object,
ByVal e As PropertyChangedEventArgs) _
Implements INotifyPropertyChanged.PropertyChanged
End Class
如您所见,我正在更改已编辑的值,以便当用户在 中输入文本时,它会在其开头TextBox
添加 a 进行更新。_
发生的情况是,当我在中键入123456789
结果时TextBox
:_________987654321
不是_________123456789
预期的那样。
有什么好的方法可以解决它?
我不想让我的整个代码被 KeyUp 等事件弄脏,移动插入符号。
另一方面,我确实希望在实体级别完成此操作。
注意:我的“现实生活”功能是用破折号格式化电话号码等。