views:

705

answers:

2

In an effort to teach myself more about WPF, I'm trying to build a simple application. Where I work we have some hardware that has a bank of 8 dip switches used for setting an address from 0 to 255 (as an 8-bit number). It's a simple concept that I would like to build into a WPF windows application that would allow users to see the dip switch settings for a given address. I built a similar app in WinForms that would simply show/hide pictures of dip switches. I'd like to port it to WPF and do something more elegant.

I've done some simple databinding with strings and numbers, making them show up in labels, and I've done some introductory work with user controls.

What I would like to do is create a user control that mimics a single dip switch, which would have a "State" property that would be a boolean true or false. I could then place 8 of them in my window and tie them to my code.

In the XAML, I would have a drawing of the dip switch, and the sliding part would move to the on or off position depending on the true/false value of the State property. Once I figure that out, maybe I could animate it later.

At this point I'm looking for a conceptual overview... how would I create this property in a user control, and have it's value affect the position of the graphical switch?

Thanks.

A: 

One way you could do this is to use the Control Template to customize the appearance of the WPF RadioButton class. The code for the RadioButton Control Template is available on msdn.

You should be able to create your own UserControl, then paste this code into the UserControl.Resources node, and then start modifying the look and feel of the RadioButton while maintaining all of its properties, which should make it easy to use as a boolean indicator.

The style of the "Bullet" could be easily replaced by images or vector graphics of flipped or unflipped dip switches.

Guy Starbuck
+2  A: 

You can easily create a DIP switch control by replacing the template of a CheckBox, if you want to know how to create your own templatable controls read "Templates for Uncommon Controls" by Charles Petzold at http://msdn.microsoft.com/en-us/magazine/cc135986.aspx

Nir