0

The standard RadioButton does not support setting the color of the ellipse. So, I took a radiobutton template from this location as a basis for a custom RadioButton: RadioButton Styles and Templates

<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
            <GradientStop Color="{DynamicResource ControlLightColor}" />
            <GradientStop Color="{DynamicResource ControlMediumColor}" Offset="1.0" />
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

ControlLightColor and ControlMediumColor are defined as:

<Color x:Key="ControlLightColor">#ffff9a</Color>
<Color x:Key="ControlMediumColor">#ffff9a</Color>

Which gives us a yellow-ish ellipse.

How can I alter this color in the codebehind?

Regards,

Michel

Pang
  • 9,564
  • 146
  • 81
  • 122
Michel van Engelen
  • 2,791
  • 2
  • 29
  • 45

2 Answers2

0

Create a style by following this: Creating a Style in code behind

then assign it to your element.Style

You can also access resources by

Resources["mykey"]
Community
  • 1
  • 1
jrb
  • 1,708
  • 2
  • 13
  • 20
  • I updated the Application.Current.Resources("ControlLightColor") with a different color, nothing happens. I also did a RaisePropertyChanged on my binding element, also doesn't work. – Michel van Engelen May 24 '12 at 10:17
  • maybe you have to call InitializeComponent(); It's probably better to crate two styles and just switch style. Or bind the gradientstop color to a property (MVVM) – jrb May 24 '12 at 11:14
  • Right hmm, then the only thin I can think about is creating new styles.. What is it that you are trying to accomplish btw? Maybe its easier to do in xaml – jrb May 24 '12 at 12:38
  • I have a stackpanel with an itemscontrol that displays multiple radiobuttons. The radiobuttons have a color to indicate that one of them must be chosen. when a radiobutton is clicked, all radiobuttons must change to a different color indicating that the mandatory question has been answered. Without doing some templatework, it's not possible to give a radiobutton a color. I was thinking about controltemplate triggers, but all of the rb's have to get the new color, not just the one clicked. – Michel van Engelen May 24 '12 at 12:49
  • Hmm this looks promising: binding to ellipse – Michel van Engelen May 24 '12 at 13:02
0

Solution:

<Ellipse x:Name="Border" StrokeThickness="1" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.RadioButtonColor}">

        Public ReadOnly Property RadioButtonColor() As SolidColorBrush
        Get
            Dim solidColorBrush As SolidColorBrush

            If MyBusinessLogic Then
                solidColorBrush = _radioButtonNotRequiredBrush
            Else
                solidColorBrush = _radioButtonRequiredBrush
            End If

            Return solidColorBrush
        End Get
    End Property

Thumbs up for JRB for thinking along.

Pang
  • 9,564
  • 146
  • 81
  • 122
Michel van Engelen
  • 2,791
  • 2
  • 29
  • 45