XAML Dependency Property vs Regular Properties

1.4k views Asked by At

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>

Two Part question:

  1. When I use a dependency property, the Boards value will be calculated at in the designer and show 330 boards. If I use a regular property it will be 0 at design time. At runtime, either one works. Is this something we expect? If so, or if not, can someone explain to me WHY this is, so I can work around it and check to see if the rest of my code is actually working.

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

2

There are 2 answers

0
Andyz Smith On BEST ANSWER

The main reason to use dependency properties is to allow the underlying subsystem to provide additional UI/XAML/WPF based functionality, 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 can't SET LengthRequired using the Binding extension.

2) Animation 3) Styling

Same basic principle. To allow the underlying UI subsystem to animate from 0 to 100 or whatever, or to get the subsystem to pick up Styling and Themes and whatever, it must be a dependency property.

1,2,3. Reasons to use dependency property. For regular properties you can jam in an INotify.

9
ΩmegaMan On

Boards value will be calculated at in the designer

To quote MSDN (Dependency Property Overview)

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

Design mode is not runtime mode and the designer works with dependency properties because it specifically reflecting for them. The designer is not subscribing to INotifyPropertyChanged, nor does it interact with normal properties as it does dependency properties.

Should I be using a dependency property for LengthRequired?

I would put a notify property change on it, but unless you are creating a custom control, using a Dependency property is overkill.

If you set a property from XAML, you should use dependency yes

No, one can bind (set) to any property in XAML for it is just reflection. Binding is reflecting off of an item in the data context via a route (pathing info) provided.

But the act of binding is different than the act of getting data which occurs after binding.

But if you merely BIND to a property from XAML you can use a regular property?

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


Use these basic rules

  • Use dependency properties on controls because they are identifiable during design mode for the consumer of the control. Otherwise the user needs to find the property and cannot set the value in XAML; unlike a dependency property.
  • Any property can be bound to, but changes that occur may not be telegraphed to anything which it is bound to...to provide that process good data, use INotifyPropertyChanged operations.