Expandos in xaml

This post has a couple of suggestions on ways to accomplish expandos in Xaml.  First some background …   On an HTML page you can define your own new “expando” properties on the fly, such as in this example:   <HTML>   <BODY onload=’paragraph1.innerText = paragraph1.testing’>     <P ID=’paragraph1′ testing=’1, 2, 3’/>   </BODY> </HTML>…

1

How to target a template setter at non-element content

Here’s a technique you can follow to use property triggers in a template on non-element type objects.  First, though, some background on what that means …   Take this example of a Button with a custom template which is simply a rectangle:   <Button>   <Button.Template>     <ControlTemplate TargetType=”Button”>       <Rectangle Width=’60’ Height=’40’ Fill=’Red’ x:Name=’MyRectangle’>…

3

Tip: Using XamlWriter and XamlReader to clone an object

There are multiple ways to clone objects, and multiple definitions of what “clone” should even mean.    The main issue is usually about cloning “deep” vs. “shallow”.  For example, if you have a Customer object that points to an Address object, and you clone the Customer object, does the Address object get cloned too (this…

4

Parameterized templates; and how to create reusable, custom-looking buttons without code

I’ve talked to a few people recently about parameterized templates, and so I wanted to write some of it down.  Here’s the scenario … I want to create an application that has a main window with several buttons on it.  Clicking one of the buttons navigates you to part of an app.  For example, in…

1

How to control the mouse cursor

How to set the mouse cursor in one easy step:  set the Cursor property.  For example, this markup:   <Button Cursor=”Help”>Help</Button>   … sets the cursor to be the “Help” cursor, when the mouse is over the button.    Note that since Cursor is scoped, it doesn’t apply to other parts of the page (anything…

1

A TreeView, a HierarchicalDataTemplate, and a 2D collection walk into a bar …

Bea has a handy post describing how to group items in a collection using a CollectionViewSource.  I was looking at that, and a post on the WPF forum from markovuksanovic, and for fun created a version of Bea’s example that uses a 2D collection instead of the CollectionViewSource.  (I found a bunch of HierarchicalDataTemplate examples…

9

Limited generics support in Xaml

In a post to the WPF forum, Zhou Yong had the idea to use a MarkupExtension to make it possible to create a generic dictionary (Dictionary<K,V>) from Xaml.  It’s a cool idea, so I played with it a bit, with the result shown below.  The end result is that you can do the following, for…

12

Tip: Cannot animate ‘…’ on an immutable object instance

For the most part, you can animate any property in a WPF application.  For example, the following is a rectangle that animates it’s fill color on mouse enter and leave:   <Window x:Class=”Scratch.Window1″     xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”     xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”     xmlns:local=”clr-namespace:Scratch”     Foreground=”Black”     Name=”MyWindow”      >     <Rectangle Width=”100″ Height=”100″     Fill=”Green” >     <Rectangle.Triggers>…

2

Tips & Tricks: How to undo an implicit style

You can define a style that gets used by all elements of a type, such as this case (this causes all buttons in the page to have a blue background):   <Page xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation     xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”>     <Page.Resources>         <Style TargetType=”{x:Type Button}”>       <Setter Property=”Background” Value=”Blue”/>     </Style>     </Page.Resources>     <StackPanel>…

1

A trigger for the TreeViewItem directly under the mouse

In a post to the WPF forum, martinabc wanted to define a TreeViewItem style that would trigger off whether or not the mouse was over that item.  A trigger on the IsMouseOver property sounds promising, but unfortunately IsMouseOver is true not only for the item under the mouse, but for the ancestors of that item…

11