WPF TextBox MaxLength — Is there any way to bind this to the Data Validation Max Length on the bound field?

前端 未结 5 646
北海茫月
北海茫月 2020-12-14 23:27

ViewModel:

public class MyViewModel
{
    [Required, StringLength(50)]
    public String SomeProperty { ... }
}

XAML:



        
5条回答
  •  星月不相逢
    2020-12-15 00:04

    Or you can have your model only to accept a max # chars:

    private string _MyText { get; set; }
    public string MyText { get => _MyText; set => _MyText = value?.Substring(0, 
    Math.Min(value.Length, 15)); }
    
    Text="{Binding Path=MyText}"
    

提交回复
热议问题