I have this Ellipse with a RadialGradientBrush, where the colors are bind to the colors Light and Dark:
<Grid x:Name="gridEllipse">
<Ellipse x:Name="ellipseMPCenter">
<Ellipse.Fill>
<RadialGradientBrush GradientOrigin="50,50" Center="50,50" Radius="1">
<RadialGradientBrush.GradientStops>
<GradientStop Color="{Binding Light}" Offset="0"/>
<GradientStop Color="{Binding Dark}" Offset="1"/>
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
This is my code:
public class UserControlLED : UserControl, INotifyPropertyChanged
{
private readonly Color colorGreenLight = Color.FromRgb(61, 214, 0);
private readonly Color colorGreenDark = Color.FromRgb(10, 92, 1);
private readonly Color colorRedLight = Color.FromRgb(235, 0, 0);
private readonly Color colorRedDark = Color.FromRgb(130, 0, 0);
private Color light;
public Color Light
{
get
{
return light;
}
set
{
light = value;
OnPropertyChanged(nameof(Light));
}
}
private Color dark;
public Color Dark
{
get
{
return dark;
}
set
{
dark = value;
OnPropertyChanged(nameof(Dark));
}
}
public UserControlLED()
{
InitializeComponent();
Light = colorGreenLight;
Dark = colorGreenDark;
gridEllipse.DataContext = this;
}
private async void BtStartClicked(object sender, RoutedEventArgs args)
{
Light = colorRedLight;
Dark = colorRedDark;
}
public event EventHandler<PropertyChangedEventArgs> PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
With this code, the Ellipse is colored green, even if I press the button.
When I set the DataContext in the button click event again, nothing happens. Or do I need to put this in the OnPropertyChanged method? But this still does not work.
Where and when do I need to set the DataContext?