1

I'm creating a radiobutton style. The RadioButton has a Border which hosts a ContentControl. The ContentControl has its Content property set to a Path (FemaleVector) declared in a separate ResourceDictionary. How can I change the Fill property of the path when the radiobutton IsChecked? Below is what I have so far. I am able to change the background property of the border but setting the Foreground property of the ContentControl does not change the colour of the path. (Didn't think that would work.)

<Style x:Key="Female" TargetType="{x:Type RadioButton}">
    <Setter Property="Margin" Value="0,0,5,0"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                <Border x:Name="border" Padding="7,3,7,3" Width="35" Height="35" BorderBrush="#8CD567DC" Background="#00D567DC" CornerRadius="5" BorderThickness="0.8">
                    <ContentControl x:Name="content" Content="{DynamicResource FemaleVector}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Background" TargetName="border" Value="#8CD567DC"/>
                        <Setter Property="Foreground" TargetName="content" Value="Blue"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I don't like having the long Data property of paths in my Styles, so I have moved them into a separate ResourceDictionary. Should I just put the Path back into my Style instead of keeping it in a separate ResourceDictionary?

Edit: similar questions are here and here.

Community
  • 1
  • 1
Blake Mumford
  • 17,201
  • 12
  • 49
  • 67

1 Answers1

2

If the style is not reused somewhere, I would personally keep it in local style resources section. That way you see bigger picture. Otherwise it would be wise to keep it in ResourceDictionary :)

Either way you should be able to change Fill property with:

<Setter Property="Content.Fill" TargetName="content" Value="Blue"/>

If this is not working, I advise few ways more: You can use Trigger.EnterActions<> in Xaml. Perhaps setting property through animation will have better effect? ControlTemplate triggers with setters are sometimes way limiting.

There's also relative binding. But you gotta be careful with that. (If you pla to make it reusable) In your FemaleVector style, you can bind Fill against ContentControl Foreground. Look for RelativeBinding in Google.

And then there's property inheritance. If you set Fill color in FemaleVector, you need to do it with style. Such as:

<Style>
<Setter Property="Fill" Value="BLACK" />
</Style>

you can later set ContentControls Style and add trigger there, like:

<ContentControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding={Binding IsChecked, RelativeSource={RelativeSource AncestorType=RadioButto}} Value=TRUE>
<Setter Property="Path.Fill" Value="BLACK" />
</DataTrigger>

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78