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

Brief anatomy of a ListBox

ListBox has properties on it that allow you to control how the items in the list box get displayed.  Those properties make sense if you understand some basic concepts of the ListBox, but sometimes I forget, so I wanted to post a picture on my wall.  Except I’m trying to live a paper-free life, and…

2

Xaml Inspired Markup

A lot has been written about how Xaml maps tags and attributes to objects and properties.  (Here’s a standard Xaml overview.)  But it’s not necessary that Xaml actually be used to create .Net objects.  For example, XPS is markup that can be treated like Xaml and used to create/initialize objects, but it can also be…

1

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

Sharing, and not sharing, Setter.Value in a Style or Template

Setter values in a style or template get shared, which is good for performance, but impacts how some features work.  Take this example with a Button style:   <Page xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation“ >   <Page.Resources>     <Style TargetType=“Button“>       <Setter Property=“Background“ Value=“Red“ />     </Style>   </Page.Resources>     <StackPanel>     <Button>Click</Button>     <Button>Clack</Button>   </StackPanel> </Page>…

0

The Loaded event and the Initialized event

In WPF, controls have both a Loaded even and an Initialized event.  Initializing and loading a control tend to happen at about the same time, and consequently these events fire at roughly the same time.  But they have slightly – though important – different meanings, and the differences can be a source of confusion.  So…

5