Silverlight: How do you implement validation in custom controls?

后端 未结 1 1719
暗喜
暗喜 2021-02-10 01:16

How do you implement validation in custom controls? I am looking to replicate the standard validation logic you would see with a TextBox data-bound to a model or view-model that

1条回答
  •  一整个雨季
    2021-02-10 01:21

    To implement validation you should add the "ValidationStates" group to the VisualStateManager of the control.

    I will illustrate the simple custom control TestControl with the TestProperty property.

    Style in the Generic.xaml, depending on the state displays the blue text or the red border with the first error message:

    
    

    There are 3 states:

    • Valid - No validation errors.
    • InvalidFocused - Applied when you set the focus to the control in the invalid state. Default controls display the red popup as well as the red border in this state, but in my particular example I don't display it for simplicity. Users can invoke this state by using the Tab keyboard button or by clicking a focusable inner control like TextBox.
    • InvalidUnfocused - Applied when the control in the invalid state but isn't focused.

    Here is the code of the control, it contains only one property:

    public class TestControl : Control
    {
        public TestControl()
        {
            this.DefaultStyleKey = typeof(TestControl);
        }
    
        public string TestProperty
        {
            get { return (string)GetValue(TestPropertyProperty); }
            set { SetValue(TestPropertyProperty, value); }
        }
    
        public static readonly DependencyProperty TestPropertyProperty =
            DependencyProperty.Register("TestProperty", typeof(string), typeof(TestControl), new PropertyMetadata(null));
    }
    

    After that if you use the IDataErrorInfo, the correct xaml is:

    
    

    For the INotifyDataErrorInfo, the correct xaml is even simplier:

    
    

    0 讨论(0)
提交回复
热议问题