Creating a DIP Switch Control in WPF

In an attempt to teach me more about WPF, I am trying to create a simple application. Where I work, we have equipment that has a bank of 8 dip switches used to set the address from 0 to 255 (as an 8-bit number). This is a simple concept that I would like to embed in a Windows WPF application that will allow users to see the dip switch settings for a given address. I created a similar application in WinForms that will just show / hide pictures of dip switches. I would like to put it in WPF and do something more elegant.

I did some simple data bindings with strings and numbers making them appear in labels, and I did some introductory work with custom controls.

What I would like to do is create a custom control that simulates a single dip switch that will have a State property that would be a boolean true or false. Then I can place 8 of them in my window and bind them to my code.

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

At this point I'm looking for a conceptual overview ... how would I create this property in a custom control and affect its value on the position of the graphic radio button?

Thank.

0


source to share


2 answers


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



+2


source


One way to do this is to use a control pattern to customize the appearance of the WPF RadioButton class. The code for the RadioButton control template is available at msdn .

You should be able to create your own UserControl and then insert this code into the UserControl.Resources node and then start changing the appearance of the RadioButton while keeping all of its properties, which should make it easier to use as a boolean indicator.



The "Bullet" style can be easily replaced with images or vectors of flip or non-directional dip switches.

0


source







All Articles