Compartilhar via


Estilos e Templates in-line

Windows Presentation Foundation (WPF) provides Style objects and template objects (FrameworkTemplate subclasses) as a way to define the visual appearance of an element in resources, so that they can be used multiple times. For this reason, attributes in XAML that take the types Style and FrameworkTemplate almost always make resource references to existing styles and templates rather than define new ones inline.

Limitations of Inline Styles and Templates

In Extensible Application Markup Language (XAML), style and template properties can technically be set in one of two ways. Você pode usar a sintaxe de atributo para fazer referência a um estilo que foi definido dentro de um recurso, por exemplo <objeto Style="{StaticResource myResourceKey}" .../>. Or you can use property element syntax to define a style inline, for instance:

<object>

<object.Style>

<Style .../>

</object.Style>

</object>

The attribute usage is much more common. A style that is defined inline and not defined in resources is necessarily scoped to the containing element only, and cannot be re-used as easily because it has no resource key. Em geral um estilo definido pelo recurso é mais versátil e útil e é mais acompanhando geral Windows Presentation Foundation (WPF) princípio de modelo de programação de separar a lógica do programa no código do design na marcação.

Usually there is no reason to set a style or template inline, even if you only intend to use that style or template in that location. Most elements that can take a style or template also support a content property and a content model. If you are only using whatever logical tree you create through styling or templating once, it would be even easier to just fill that content property with the equivalent child elements in direct markup. This would bypass the style and template mechanisms altogether.

Other syntaxes enabled by markup extensions that return an object are also possible for styles and templates. Two such extensions that have possible scenarios include TemplateBinding and Binding.

Consulte também

Conceitos

Styling and Templating