user1151923
user1151923

Reputation: 1872

make explicit-only xaml style OR style only applied in a region

When I declare a style in the Resources of the root of a user control it is applied to all controls of the target type in that XAML file. How can I make a style that is: * Only used if I explicitly state it so OR * Limited to a certain region in a user control (such as putting a Grid in a Grid and then declaring a style in the second grid will only be applied to that grid). Is there any such container I can use and that has no effect whatsoever on the UI, but is just a container for the style?

Upvotes: 0

Views: 100

Answers (1)

Wes Cumberland
Wes Cumberland

Reputation: 1338

To apply it to a whole subtree, you can declare the style in the resources of any element and it will only apply to that element (if it's the right type) and elements of the right type in its subtree. (Put it in your second-level Grid's <Grid.Resources> for example)

To assign it to specific elements, you can put it in an ancestor element's Resource Dictionary with an x:Key attribute (<Style x:Key="MyStyle">) and then point desired sub-elements at it using their Style attribute (Style="{StaticResource MyStyle}"). If you give it an explicit x:Key attribute, it will not match implicitly based on type.

Or if you don't want to use a resource at all, you can define a style inline in any element you want (<Grid><Grid.Style><Style>...</Style></Grid.Style><Grid>) and it will only apply to that element.

Upvotes: 1

Related Questions