1

I am reading WPF 4 Unleashed and I am obviously new to WPF (and C# & .NET in general)

In the book, found the following snipped of code:

<StackPanel TextElement.FontSize="30" TextElement.FontStyle="Italic"
            Orientation="Horizontal" HorizontalAlignment="Center">
   <Button MinWidth="75" Margin="10">Help</Button>
   <Button MinWidth="75" Margin="10">OK</Button>
</StackPanel>

TextElement.FontSize is an attached property. I don't understand why is it attached property ? (Though, I do understand the concept of dependency property)

In Attached Properties Overview on MS site, there is another snipped of code.

<DockPanel>
  <CheckBox DockPanel.Dock="Top">Hello</CheckBox>
</DockPanel>

In this case, it makes sense as to why DockPanel.Dock is an attached property - DockPanel class contains dependency property DockProperty.

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
newprint
  • 6,936
  • 13
  • 67
  • 109

1 Answers1

2

If you're new to C#, I strongly suggest you start by doing some Hello, World! type of stuff in console applications before trying to get into complex WPF GUI stuff.

WPF is a complex framework not really suitable for the unexperienced. You must have a strong background in C# and OOP in general in order to learn MVVM, which is what you must learn in order to use WPF properly.

That said, StackPanel does not have a FontSize property because it's a Panel and not a Control, which is where the FontXXX properties are defined. That's why you can optionally define the TextElement.FontSize Attached Property, which are child controls will inherit due to Dependency Property Value Inheritance in the Visual Tree.

As an aside, that book was in a former coworker's desk, so I grabbed it and rapidly browsed thru it. I didn't find a single mention to MVVM, which at this point I consider a fundamental part of the WPF learning curve.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • I read this book `WPF 4 Unleashed. Adam Nathan`, and also did not find any mention of MVVM. That way the book just a set of basic information about the WPF, which will start in the study of this technology, but unfortunately did not really learn how to create a serious application. – Anatoliy Nikolaev Jan 27 '14 at 02:54
  • 6
    The buzzword-based opinions like this is what makes WPF appear complex and frightening to many of the beginners. Also, to grasp even more wisdom of MVVM, see this: http://stackoverflow.com/questions/1098023/how-have-you-successfully-implemented-messagebox-show-functionality-in-mvvm – ezolotko Jan 27 '14 at 03:13
  • 1
    I wish the book mentioned that TextElement is a helper class that can be used to control text appearance. WPF is complex framework, but what is simple nowadays ? – newprint Jan 27 '14 at 03:31
  • More on `TextElement` [here](http://msdn.microsoft.com/en-us/library/aa970786(v=vs.110).aspx) – Mahesha999 Apr 04 '14 at 13:23