I am writing a user control that simulates an LED. The LED currently just has two states, but the goal is to have four (on, off, disconnected from signal, disabled). I want to be able to set the control property State to match the possible states as follows.
<local:LEDControl State="ON" HorizontalAlignment="Left" Height="100" Margin="374,131,0,0" VerticalAlignment="Top" Width="100"/>
Setting this property should set the color of the LED too some predefined colors:
public partial class LEDControl : UserControl
{
private (string ON, string OFF) state_colors = ("#5DC211", "#FF0808");
private string _state;
public string State
{
get
{
return _state;
}
set
{
if (value == "ON")
{
_state = state_colors.ON;
}
else //should check for valid input
{
_state = state_colors.OFF;
}
}
}
public LEDControl()
{
InitializeComponent();
this.DataContext = this;
State = "OFF";
}
}
And here is the xaml:
<UserControl x:Class="SV3eStatusWindow.LEDControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SV3eStatusWindow"
mc:Ignorable="d"
d:DesignHeight="45" Width="44.628"
>
<Grid x:Name ="LayoutRoot" >
<Ellipse Fill="{Binding State}" HorizontalAlignment="Center" Height="35" Stroke="Black" VerticalAlignment="Center" Width="35"/>
</Grid>
</UserControl>
I get an error saying I can only bind to a dependency property. But I am confused about how to write a dependency property to have the get and set functionality I have above.
I would like to know how to do what I am trying to do above for educational purposes. However, I acknowledge that this might not be the best design because I am new to WPF and I am open to suggestion.