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

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

ViewModel:

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

XAML:



        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 23:48

    While I'm not going to write the code out completely myself, one idea is to create your own MarkupExtension that will take the property name and reflect over looking for a StringLengthAttribute.

    If the attribute exists, attempt to bind the target to that value (using reflection). If not, then bind 0 to the target value (0 is default, i.e. no max).

提交回复
热议问题