WPF e l'ereditarietà degli stili

Gli Style di WPF sono certamente uno strumento molto comodo. Una caratteristica interessante degli Style è che uno ne può estendere un altro.

Supponiamo di avere la seguente definizione:

<Style x:Key="MainControlStyle" TargetType="{x:Type Control}" > <Setter Property="FontSize" Value="10" /> <Setter Property="FontWeight" Value="Normal" /> <Setter Property="Padding" Value="2" /> </Style> <Style TargetType="{x:Type Label}" BasedOn="{StaticResource MainControlStyle}" > <Setter Property="FontWeight" Value="Bold" /> </Style>

Con la proprietà BasedOn del secondo Style indichiamo che il secondo stile "eredita" le proprietà impostate da MainControlStyle.

Cosa abbiamo prodotto? Abbiamo due stili: il primo applicabile ad ogni Control che imposta FontSize, FontWeight e Padding; il secondo, che verrà applicato ad ogni Label, che "sovrascrive" il FontWeight impostato dallo stile padre, forzandolo a Bold.

Per completezza faccio un esempio in cui il primo stile non abbia una chiave che lo identifichi:

<Style TargetType="{x:Type Label}" > <Setter Property="FontSize" Value="10" /> <Setter Property="FontWeight" Value="Normal" /> <Setter Property="Padding" Value="2" /> </Style> <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" > <Setter Property="FontWeight" Value="Bold" /> </Style>

Una sola nota: il TargetType dello stile che eredita deve essere compatibile con il TargetType dello stile padre, in sostanza lo stesso tipo o una sottoclasse.

 

Matteo

 

Technorati Tag: ,,,

Print | posted @ mercoledì 11 marzo 2009 14:50

Comments on this entry:

Gravatar # re: WPF e l'ereditarietà degli stili
by taoruankeji at 19/08/2010 09:21

PDF to TIFF Converter is a professional PDF to images program which let you convert PDF to TIFF and lots of image formats with high quality, such as JPEG, PNG, GIF, BMP, PCX, TGA. As to convert PDF to TIFF, you can freely set output encoder, including lossless, RLE, FAX G3, etc. Using this PDF to TIFF Converter, you alos can choose colorful, grey and black as output color. Reset resolution is applicable as well.
PDF to XLS Converter
PDF to EPUB Converter
Convert PDF to GIF
Gravatar # re: WPF e l'ereditarietà degli stili
by courseworks at 25/12/2010 14:50

Nobody is assured about the situation that occurs in his/her life. Not every one has a lot of free time to create perfect course work. Nonetheless, every one is able to buy coursework help.
Gravatar # re: WPF e l'ereditarietà degli stili
by writing dissertation introductio at 30/03/2012 21:02

Real education must ultimately be limited to men who insist on knowing, the rest is mere sheep-herding.
Gravatar # re: WPF e l'ereditarietà degli stili
by pdftojpeg at 20/04/2012 08:38

PDF creator is a powerful PDF writer, you can easily create PDFs from any program that is able to print. You can use PDF Creator like a printer in Word, Excel, Powerpoint or any other Windows application.
Gravatar # re: WPF e l'ereditarietà degli stili
by Digital Marketing Agency at 21/01/2017 09:19

Such a outstanding website supplies several in fact valuable home elevators which frequently could. Very best calls for designed for supplying which frequently could near to individuals!
Gravatar # re: WPF e l'ereditarietà degli stili
by modern furniture at 07/03/2017 13:59

I’ve already been contemplating creating a remarkably similar article throughout the last short time, I’ll probably retain of which smaller in addition to good in addition to url to this type of instead in the event that's cool. Thanks!
Gravatar # re: WPF e l'ereditarietà degli stili
by GamingMarket at 05/12/2017 09:39

Despite the fact that when i confirm this sort of specifics. I'm sure Individuals spot numerous volume with a specific end goal to build up this sort of specifics. In spite of the fact that when i advantageous work.
Comments have been closed on this topic.