Charles Petzold



XAML XOR Gate Works Also

September 18, 2007
New York, N.Y.

A few days ago, when I realized I didn't know how common three-way switches worked of course I knew how I'd do it if I wiring it up on a circuit board: I'd use an Exclusive OR gate, which can be constructed from an OR, NAND, and AND gates, as this XAML file demonstrates:

XamlXor Program Screenshot

XamlXor.xaml

This approach only requires single-pole single-throw (SPST) switches rather than the SPDT three-way switches, but I guess it's more complex in other ways.

If you were writing actual WPF classes to emulate logic gates, you'd define properties named Input1, Input2, and Output, and you'd back them with dependency properties, of course, because you'd want them to be bindable.

If you're attempting an all-XAML solution such as this, you don't have that convenience, and you need to, let's say, appropriate some existing properties. (That sounds so much better than filch.) You have to find three properties of type bool that you can set and reset at will without screwing anything up. For the gates, I chose ForceCursor and AllowDrop to be the two inputs, and Focusable for the output. The Focusable property is also used in the Path elements (for wires and junctions) to determine the color of the wire.