XAML Dependency Property vs Regular Properties

If I have this:

    public class BoardCalc : FrameworkElement
    {
        public BoardCalc()
        {
            this.Loaded += BoardCalc_Loaded;
        }

        void BoardCalc_Loaded(object sender, RoutedEventArgs e)
        {
            Boards = Math.Floor(LengthRequired / 16);
            BoardsRequired2 = Math.Floor(LengthRequired / 16);
        }

        public Double LengthRequired { get; set; }

        private Double _Boards;
        public Double Boards
        {
            get
            {
                return _Boards;
            }
            set
            {
                _Boards = value;
            }
        }


        //public Double Boards
        //{
        //    get { return (Double)GetValue(BoardsProperty); }
        //    set { SetValue(BoardsProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for Boards.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty BoardsProperty =
        //    DependencyProperty.Register("Boards", typeof(Double), typeof(BoardCalc), null);

        public Double BoardsRequired2
        {
            get { return (Double)GetValue(BoardsRequired2Property); }
            set { SetValue(BoardsRequired2Property, value); }
        }

        // Using a DependencyProperty as the backing store for BoardsRequired2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoardsRequired2Property =
            DependencyProperty.Register("BoardsRequired2", typeof(Double), typeof(BoardCalc), null);



    }

      

And I do this:

 <StackPanel xmlns:Boards="clr-namespace:BoardsUtil" >
             <Boards:BoardCalc x:Name="boardCalc1"
                LengthRequired="5280"  />

             <TextBlock Text="{Binding ElementName=boardCalc1, Path=Boards}" />

             <TextBlock Text="{Binding ElementName=boardCalc1, Path=BoardsRequired2}" />

         </StackPanel>

      

A question with two parts:

  • When I use the dependency property the Boards value will be calculated in the designer and display 330 tables. If I use a regular property it will be 0 at design time. During work, one works. Is this what we expect? If this is the case, or if not, can someone explain to me WHY this is, so I can work around it and check if the rest of my code actually works.

  • Should I use a dependency property for LengthRequired? If you set the property from XAML you should use the yes dependency? But if you just BIND to property from XAML, can you use a regular property? Is this the behavior we see here? Is this what we expect? Not? Yes? WHY, so I can decide what to do.

+3


source to share


2 answers


The main reason for using dependency properties is to allow the underlying engine to provide additional functionality based on the UI / XAML / WPF interface, namely:

1) Binding. In this code:

<Slider x:Name="slid1" Maximum="5280" Minimum="16" Value="250" />
<Boards:BoardCalc x:Name="boardCalc1"
            LengthRequired="{Binding ElementName=slid1,Path=Value"  />

      

LengthRequired must be a dependency property. You can set LengthRequired like this

LengthRequired = "5280"

      

and you can do this



Text={Binding ElementName=boardCalc1, Path=LengthRequired} ...

      

but you cannot SET LengthRequired with the Binding extension.

2) Animation 3) Styling

The same basic principle. To allow the underlying UI subsystem to animate from 0 to 100 or whatever, or for the subsystem to pick up style and themes and whatever, it must be a dependency property.

1,2,3. Reasons for using the dependency property. For normal properties, you can hush in INotify.

+3


source


The board value is calculated in the designer

To quote MSDN ( Dependency Properties Overview )

The purpose of dependency properties is to provide a way to calculate the value of a property based on the value of other inputs.

Design mode is not runtime, and the designer works with dependent properties because he specifically reflects them. The designer does not subscribe to INotifyPropertyChanged and does not interact with normal properties as it has dependency properties.

Should I use a dependency property for LengthRequired?

I would add a notification property change to it, but unless you are creating a custom control, using the Dependency property is overkill.

If you set the property from XAML you must use the yes dependency



No, you can bind (set) to any property in XAML, because it's just a reflection. The binding is reflected from the element in the data context via the provided route (path information).

But the act of binding is different from the act of receiving data that occurs after binding.

But if you just BIND for a property from XAML, can you use a regular property?

Yes, but in most cases use the INotifyPropertyChanged operation to help with data binding and retrieval.


Use these basic rules

  • Use dependency properties for controls as they are identified in design mode to the control consumer. Otherwise, the user must find the property and cannot set the value in XAML; as opposed to the dependency property.
  • Any property can be bound, but changes that occur cannot be wired to anything they are bound to ... to provide good process data, use operations INotifyPropertyChanged

    .
+2


source







All Articles