Click here to Skip to main content
15,895,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I have this WPF style:

XML
<Style TargetType="RadioButton" x:Name="Toggle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Rectangle>
                            <Rectangle.Style>
                                <Style TargetType="Rectangle">
                                    <Style.Triggers>
                                        <Trigger Property="IsChecked" Value="True">
                                            <Setter Property="Fill" Value="{TemplateBinding Foreground}" />
                                        </Trigger>
                                        <Trigger Property="IsChecked" Value="False">
                                            <Setter Property="Fill" Value="{TemplateBinding Background}" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Rectangle.Style>
                        </Rectangle>
                        <ContentPresenter />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


Those lines:

XML
<Trigger Property="IsChecked" Value="True">
                                                <Setter Property="Fill" Value="{TemplateBinding Foreground}" />
                                            </Trigger>
                                            <Trigger Property="IsChecked" Value="False">
                                                <Setter Property="Fill" Value="{TemplateBinding Background}" />
                                            </Trigger>

Do not work as they are nested in a Rectangle and not in the Radiobutton.

How can I make the trigger refer to the RadioButton?

Thanks a lot!!

Jymmy097
Posted
Comments
johannesnestler 9-Sep-14 9:56am    
so why not set the Trigger to the RadioButton style (I mean in the ControlTemplate)? If you set it to the rectangle no wonder it belongs to it... And wyh are you using a style for the rectangle inside the control template - I can't see any use for that - can you enlighten me?
LLLLGGGG 9-Sep-14 12:54pm    
I've solved the problem, but thanks to your comment...
If you want to put the answer here, I'll mark it as a solution

1 solution

XML
<style targettype="RadioButton" x:key="Toggle" xmlns:x="#unknown">
        <setter property="Margin" value="0,2" />
        <setter property="Template">
            <setter.value>
                <controltemplate targettype="RadioButton">
                    <border borderbrush="{TemplateBinding BorderBrush}" borderthickness="{TemplateBinding BorderThickness}">
                        <grid>
                            <rectangle fill="{TemplateBinding Background}" />
                            <contentpresenter content="{TemplateBinding Content}" />
                        </grid>
                    </border>
                </controltemplate>
            </setter.value>
        </setter>
        <style.triggers>
            <trigger property="IsChecked" value="True">
                <setter property="Background" value="{StaticResource radioChecked}" />
            </trigger>
            <trigger property="IsChecked" value="False">
                <setter property="Background" value="{StaticResource radioUnchecked}" />
            </trigger>
        </style.triggers>
    </style>
    <lineargradientbrush x:key="radioChecked" startpoint="0,0" endpoint="0,1" xmlns:x="#unknown">
        <gradientstop color="#EEEEEE" offset="1" />
        <gradientstop color="#999999" offset="0" />
    </lineargradientbrush>
    <lineargradientbrush x:key="radioUnchecked" startpoint="0,0" endpoint="0,1" xmlns:x="#unknown">
        <gradientstop color="#EEEEEE" offset="0" />
        <gradientstop color="#999999" offset="1" />
    </lineargradientbrush>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900