我在 wpf mvvm 應用程式中作業,我也使用 Community toolkit.mvvm。在這里,我為文本框實作了 Observable 驗證器,但它不會影響用戶界面,
視圖模型:
public partial class UserViewModel : ObservableValidator
{
[ObservableProperty]
[Required(ErrorMessage ="Name is Required")]
[MinLength(3)]
private string name= "";
}
xml:
<TextBox Text="{Binding Name,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
當我在文本框中鍵入少于 3 個字符并保留它時,它不起作用,TextBox 接受每個范圍內的字符,也接受空字符。
uj5u.com熱心網友回復:
確認系結正在作業。不需要ValidatesOnDataErrors將系結的屬性設定為:true
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}"/>
uj5u.com熱心網友回復:
謝謝你!@mm8先生
我只是錯過了一個屬性,--> [NotifyDataErrorInfo]。
現在它作業正常。
視圖模型:
public partial class UserViewModel : ObservableValidator
{
[ObservableProperty]
[NotifyDataErrorInfo]
[Required(ErrorMessage ="Name is Required")]
[MinLength(3, ErrorMessage ="Name Should be at least 3 character")]
private string username;
}
xml:
<TextBox Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527035.html
上一篇:WPF/C#,如何從另一個視窗更改主視窗的背景顏色?
下一篇:如果元素有孩子,我該如何洗掉它?
