Showing posts with label Template. Show all posts
Showing posts with label Template. 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.

Saturday, December 6, 2008

Trips&Ticks: Do you know how you can use GroupName property of RadioButton for ToggleButton in WPF?


We can use GroupName property for mutually exclusive RadioButton but what can we do if we want to use mutually exclusive ToggleButton? ToggleButton doesn't have like ability. Please, do look in RadioButton what can you see? Exactly, RadioButton is inherit from ToggleButton so we can set in Template property ControlTemplate of ToggleButton. So our RadioButtons will be look like ToggleButtons besides will have GroupName property.

Wednesday, July 30, 2008

My version of Snoop (+ 2 features) of WPF


I think that every WPF developer knows that is Snoop. This is helpful tool (with source code (!!) ) for visual debugging of WPF's app at runtime. It is good but I was in need of some expansions.
  1. I needed to see Template of controls in visual tree.
  2. I needed to keep a/some couple (value and property) of control in visual tree.
I implemented this issues for self purpose and want to impart this to all.

At first. My coworker Ivan and ex-coworker Eugene implemented like issue. But their solution didn't satisfy me. It had low performance. I re-implement it and in my solution convert to xaml come true after click to "Show XAML!" button only. It increase performance in comparison with previous solutions.


For second feature I used code of Beatriz Costa that allows you to drag and drop items between data bound ItemsControls (I added a little modification for my purpose only). I added new PropertyGrid. We can drag a item from main PropertyGrid to keep PropertyGrid.

In last PropertyGrid property saves relation with its owner even if it contains different owners of properties.

Here you can find the project with this features built using VS2008.

Enjoy ;)

Monday, September 3, 2007

Correct use DataTemplateSelector.

In the many inherited from FrameworkElement in WPF contain a useful dependency property *TemplateSelector type of DataTemplateSelector. It can set our inherit from DataTemplateSelector class and override the method SelectTemplate(). This allow dynamic generate template for a content. Content-data we can get a elementary binding. And now feature: we can use all binding (even that what refet to ElementName), except TemplateBinding, therefore RelativSourceMode doesn't equal TemplatedParent.

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.