0

I read this post and this one about depency properties and I am confused, are all properties in XAML dependency properties ? and what about normal properties we define in C# ? I mean like this : Public int num {get; set;} .

Because the normal properties in C# have some features that they mentioned as a option for dependency property for example I can bind their values to a textbox value.

And if you can make a simple example I will be grateful.

  • No. This answer on one of your links helps I think... https://stackoverflow.com/a/5390402/3225 – kenny Dec 28 '18 at 13:51
  • @kenny All wpf properties are dependency properties, right? but all C# properties are not, yes? –  Dec 28 '18 at 13:55
  • 1
    The answer below is fine as well as the answer I suggested on your link. A "WPF Property", not sure what you mean? BUT C# properties are not dependency properties. – kenny Dec 28 '18 at 14:00
  • I mean the properties in XAML which can be bind or use in style and ... , in one of the answeres they mentioned these option for dependency property. –  Dec 28 '18 at 14:03
  • 2
    you can bind to 'normal' properties, but they will not have behaviors and interact in all the ways required to perform some of the things that dependency properties can support (see capabilites in the answer I linked to https://stackoverflow.com/questions/5390331/what-is-a-dependency-property-what-is-its-use/5390402#5390402_. – kenny Dec 28 '18 at 14:05

1 Answers1

2

I feel that the other posts answer what a Dependency Property is fairly well so I will address your question showing how to make a Dependency Property, hopefully that will help.

Are all properties in XAML dependency properties?

No, Dependency properties must be specified as such. See below...

public class MyDataGridControl : DataGrid
{
    public string SomeName
    {
        get { return (string)GetValue(SomeNameProperty); }
        set { SetValue(SomeNameProperty, value); }
    }

    public static readonly DependencyProperty SomeNameProperty = 
        DependencyProperty.Register(
            nameof(SomeName), typeof(string), typeof(MyDataGridControl),
            new PropertyMetadata(null));
}

In the example above, I have created a class that inherits from DataGrid to make my own DataGrid control. I have created the "normal property" SomeName. I then register SomeName as a Dependency Property. Notice that while SomeName is a "normal property", the getter and setter are referencing the SomeNameProperty Dependency Property.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Sudsy1002
  • 588
  • 6
  • 21
  • Your confusion appears to be that the property is appearing in xaml. For a property to appear in xaml, it only needs to be public and have a setter. It DOES NOT need to be a dependency property. – Sudsy1002 Dec 28 '18 at 14:13
  • I wanna add that one shouldn't put **any** code into CLR property wrapper as it's possible to get/set dependency property *directly*. – JohnyL Dec 28 '18 at 16:40
  • For instance, `Window.Title = "My Title"` could be called as `Window.SetValue(Window.TitleProperty, "My Title)"` – JohnyL Dec 28 '18 at 16:42