473,651 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a simple LED Control - WPF

7 New Member
Hi!

Let's say I wanted to create a very simple control that is nothing but a LED. That LED can be active or inactive. Depending on that state its color changes.

What is WPFs best practice to do so? Because I dont like the control the way it is right now at all.

LEDControl.xaml :
Expand|Select|Wrap|Line Numbers
  1. <UserControl x:Class="WpfPanelClient.Views.LEDControl"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SnapsToDevicePixels="True" Margin="4">
  4.  
  5.     <Ellipse Name="ledEllipse" Width="{Binding Path=ActualHeight, ElementName=ledEllipse}" Stroke="Black" Fill="{Binding Fill, Mode=OneWay}"/>
  6. </UserControl>
  7.  
LEDControl.xaml .cs:
Expand|Select|Wrap|Line Numbers
  1.     public partial class LEDControl : UserControl, INotifyPropertyChanged
  2.     {
  3.         public static DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof (bool),
  4.                                                                                         typeof (LEDControl),
  5.                                                                                         new PropertyMetadata(
  6.                                                                                             new PropertyChangedCallback(
  7.                                                                                                 OnChangedIsActive)));
  8.  
  9.         private Brush fill;
  10.  
  11.         public LEDControl()
  12.         {
  13.             InitializeComponent();
  14.             DataContext = this;
  15.             IsActive = false;
  16.         }
  17.  
  18.         public bool IsActive
  19.         {
  20.             get { return (bool) GetValue(IsActiveProperty); }
  21.             set { SetValue(IsActiveProperty, value); }
  22.         }
  23.  
  24.         public Brush FillActive { get; set; }
  25.  
  26.         private Brush fillInActive;
  27.         public Brush FillInActive
  28.         {
  29.             get { return fillInActive; }
  30.             set { fillInActive = value;
  31.                 Fill = value; }
  32.         }
  33.  
  34.         public Brush Fill
  35.         {
  36.             get { return fill; }
  37.             set
  38.             {
  39.                 fill = value;
  40.                 OnPropertyChanged("Fill");
  41.             }
  42.         }
  43.  
  44.         #region INotifyPropertyChanged Members
  45.  
  46.         public event PropertyChangedEventHandler PropertyChanged;
  47.  
  48.         #endregion
  49.  
  50.         private static void OnChangedIsActive(DependencyObject dependencyObject,
  51.                                               DependencyPropertyChangedEventArgs eventArgs)
  52.         {
  53.             var active = (bool) eventArgs.NewValue;
  54.             var control = ((LEDControl) dependencyObject);
  55.  
  56.             control.Fill = active ? control.FillActive : control.FillInActive;
  57.         }
  58.  
  59.         private void OnPropertyChanged(string property)
  60.         {
  61.             if (PropertyChanged != null)
  62.                 PropertyChanged(this, new PropertyChangedEventArgs(property));
  63.         }
  64.     }
  65.  
View:
Expand|Select|Wrap|Line Numbers
  1.             <Controls:LEDControl x:Name="ledControl" Grid.Column="3" Grid.Row="1"  FillActive="LightGreen" FillInActive="Black" IsActive="False"/>
  2.  
  3.  

Thanks!

Kevin
Nov 24 '09 #1
0 3267

Sign in to post your reply or Sign up for a free account.

Similar topics

4
22164
by: Tom Rathbun | last post by:
This is probably simple but it has stumped me. I want to create objects at runtime for example: A program that would allow you to draw lines on a form. For each new line I would like to create a new line object in an array the same way you would a simple variable. if it was just a variable I could dim a(0) as single ..
6
3687
by: Vinay | last post by:
Hi all, Can we create an ActiveX (.OCX) control in ATL project type? Is project type for creation of .OCX file can only be ActiveX MFC type? If yes, Could you suggest me some Links? My Requirement: To create an ActiveX control that could be include in a web Page. This control has a DataGrid on it. Iam unable to do so in MFC ActiveX projects. Im using VS 6.0.
3
3510
by: Bill | last post by:
I have a seating chart web form that has over 50 entry field controls (tables/booths) where I use a DropDownList box to select a single company name from a single large list of organizations (200 plus conference attendees). All web form datavalues will be one of the same 200 organizations in this list. I would like to avoid creating 50 separate exact copies of the same DataSet object. Can you help? Q. Exactly how do I use the same...
4
1547
by: Daylor | last post by:
hi. i have multi thread application in vb.net is there a way NET support, so i can mark the class , to be access only for 1 thread each time ? if there is , small sytax sample will help //what i need to add , so only 1 thread per time can access this class in MultiThread app.
1
1871
by: Arend Jan Nijenhuis | last post by:
Hi, I'm fairly new to .Net, but an expert in Microsoft Access VBA. For a large Access application, I want to create some sort of custom control (COM interop?) in VB.Net, to be used in an Access form. The control needs to put together a simple design drawing for building constructors, based on some values in the Access form. Can it be done?
8
3756
by: William Foster | last post by:
Good evening all, I am creating a program with the ability to select an option from a ComboBox which will then create a secondary list of options in a CheckedListBox. Previously when I have needed to create multiples of this type of user option I have simply hard coded three or four set options for the user. However, I would like to have the option for the user to have as many options available as they like. In order to do this I...
2
3932
by: chandan | last post by:
Hi, How to create an instance of user control in a class(simple) file not in Page class file? As I tried I can do it in page class file but not in simple class file. Let me know the solution!! Thanks, Chandan kumar
15
5263
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
0
2040
by: yrogirg | last post by:
Actually, I need utf-8 to utf-8 encoding which would change the text to another keyboard layout (e.g. from english to russian ghbdtn -> ÐÒÉ×ÅÔ) and would not affect other symbols. I`m totally new to python and to more or less advanced programming. I couldn`t find the answer to the question anywhere. I`ve tried create simple utf to utf codec for some symbols but it doesn`t work. Here it is.
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.