Showing posts with label Style. Show all posts
Showing posts with label Style. Show all posts

Tuesday, February 3, 2009

Trips&Ticks: Do you know how you can extract various instances of one object out of resources in WPF?


Download source files - here


I often use a style&template that is magnificent possibility of WPF. It allows to define one style out of resources to many elements. I can easily define instance of some object in resources and extract it there where I require. But it has one limitation:
For example for following:

XAML:
<Button x:Key="ButtonPrototype" />

C#:
Resources["ButtonPrototype"];
- returns always one instance (like Singlton Pattern). But what can I make if I require more than one such object? It isn't problem as XAML is very flexible and x:Shared attribute eliminates this trouble. Following code will extract various instances:

XAML:
<Button x:Key="ButtonPrototype" x:Shared="False" />

C#
Resources["ButtonPrototype"]


PS: In my opinion last realization shares traits with Prototype Pattern.

Wednesday, July 25, 2007

Custom style for a class which is inherited from the class of Selector

We need set own custom style or template for item of control which was inheritance from abstract class 'Selector' (for example: ComboBox, ListBox, TabControl) in WPF.
We can use two way:
1) We can add in control's resource some style and template. But it isn't good way, because these styles and templates have access only in boundary of control.
2) We can use inheritance from Selector ItemContainerStyle dependency property and set in it new style value.