Dependency Property Used in WPF

It's hard for me to identify good reasons for a dependency property. Why is the System.Controls.TextBox “Text” property a dependency property rather than a normal property? What benefits does it serve as a dependency property?

One of the things I'm trying to accomplish is to add the ValidationRules property to my UserControl, which will contain other validation rules. Like here:

<customControls:RequiredTextBox.ValidationRules>
                        <validators:NotNullOrEmptyValidationRule ErrorMessage="FirstName cannot be null or empty"/>
                    </customControls:RequiredTextBox.ValidationRules>

The problem is that I'm not sure if the ValidationRules property should be a DependencyProperty or just a normal property.

The above code gives the following error:

{"Cannot add element to 'ValidationRules'; the property value is null.  Error at object 'LearningWPF.ValidationRules.NotNullOrEmptyValidationRule' in markup file 'LearningWPF;component/addcustomerwindow.xaml' Line 35 Position 66."}

Here is the ValidationRules property:

 public static readonly DependencyProperty ValidationRulesProperty =
            DependencyProperty.Register("ValidationRules",
                                        typeof (Collection<ValidationRule>), typeof (RequiredTextBox),
                                        new FrameworkPropertyMetadata(null)); 

        public Collection<ValidationRule> ValidationRules
        {
            get { return (Collection<ValidationRule>)GetValue(ValidationRulesProperty); }
            set { SetValue(ValidationRulesProperty, value); }
        }
+5
source share
3 answers

:

-, , , , TextBox , , . WPF, . , , , - .

, , . , Grid.Column, Grid . , , , . , xmal .


:

, ( - , ), , , 't , Text , , - , . ; , - , , , , .

, , - WPF, . , , .

+8

, :

  • .
  • Attached Property semantics
  • , "".

, , , , , , //, + .

, ONE.

ValidationRules DependencyProperty, .

, (, ). Reflector , .NET TextBox , .

, , . , , , ;).

, DependencyProperties , , ( , ?) MSFT DependencyProperties.

Attached Properties, ( , ) DependencyProperties. Attached Property DependencyProperties .
+5

, . , Text View Model, .

+2

All Articles