我在.Net 4.5中編寫WPF MVVM應用程序,需要以下問題的幫助: 我有數值爲例如110000的文本框,並且當兩個11都被刪除UI自動填充框包含數字0.相反,我希望它保持0000. 圖片來幫助說明問題。WPF UI自動填充文本框
編輯自定義文本框將其設爲默認
static CustomTextBox()
{
var defaultMetadata = TextBox.TextProperty.GetMetadata(typeof(TextBox));
TextBox.TextProperty.OverrideMetadata(typeof(CustomTextBox), new FrameworkPropertyMetadata(
string.Empty, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
defaultMetadata.PropertyChangedCallback,
defaultMetadata.CoerceValueCallback,
true,
System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
}
物業
public double RatedVoltage
{
get { return _RatedVoltage; }
set
{
SetProperty(ref _RatedVoltage, value);
if (OnRatedVoltageChanged != null)
OnRatedVoltageChanged();
}
}
的PropertyChanged
<src:CustomTextBox VerticalAlignment="Center"
Text="{Binding TrafoProperties.RatedVoltage,
Mode=TwoWay,
ValidatesOnNotifyDataErrors=True,
NotifyOnValidationError=True}"
代碼
它將文本框綁定爲double property,當它更改爲0000時,它將自動將其更正爲0.