2

That's my code:

<Window x:Class="WpfTests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Thickness x:Key="tasksMargin">0,10,0,0</Thickness>

</Window.Resources>
<Grid>
    <DockPanel HorizontalAlignment="Left">
        <!--<Border Padding="15">-->
        <GroupBox>
            <GroupBox.Header>
                Database Tasks
            </GroupBox.Header>
            <StackPanel Width="Auto" >
                <StackPanel.Resources>
                    <Style  TargetType="RadioButton">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="RadioButton">
                                    <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                      Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                  BorderThickness="2" BorderBrush="Yellow" Background="White"/>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                                                <Setter Property="Content" Value="different txt" />
                                            </Trigger>
                                        <Trigger Property="ToggleButton.IsPressed" Value="True">
                                                <Setter Property="Content" Value="different text" />
                                            </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Margin" Value="5">
                        </Setter>
                    </Style>
                </StackPanel.Resources>

                <RadioButton  GroupName="Group1"  x:Name="button1">Option1</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button2" x:Name="button2">button2</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button3" x:Name="button3">button3</RadioButton>
            </StackPanel>
        </GroupBox>
    </DockPanel>
</Grid>

However, the IsChecked and IsPressed triggers do not get triggered. how to I declare them properly?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Greg
  • 1,227
  • 5
  • 23
  • 52
  • 1
    If I were you I'd grab a copy of some code from the web that works, and take it from there. See for example: http://anders.janmyr.com/2007/09/combining-datatriggers-and-property.html – ColinE Aug 16 '11 at 13:57
  • and the answer that actually worked for me has just been deleted. two downvotes now:D – Greg Aug 16 '11 at 14:23
  • @Greg - did it work for you? I deleted it cos I noticed it was only partially working. The IsPressed trigger wasn't working. I've re-instated the answer - but I think there's more work to be done! – IanR Aug 16 '11 at 14:26
  • yeah, is pressed doesn't seem to to work. but IsChecked works and this is the minimal requirement. If the solution for IsPressed just pops up in your head - don't hesitate to update your answer:) – Greg Aug 16 '11 at 14:30

3 Answers3

4

If you give your ToggleButton a name and then reference that in the trigger setters then it should work. So, your ControlTemplate would look something like...

<ControlTemplate TargetType="RadioButton">
    <ToggleButton Name="toggleButton" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                  Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                              BorderThickness="2" BorderBrush="Yellow" Background="White"/>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsChecked" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different txt" />
        </Trigger>
        <Trigger Property="ToggleButton.IsPressed" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different text" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
IanR
  • 4,703
  • 3
  • 29
  • 27
1

The prefixed types in the triggers are not needed, also change the bindings on the togglebutton to TemplateBindings.

H.B.
  • 166,899
  • 29
  • 327
  • 400
0

There some other solutions available, but for this problem. Do the following:

1) Give Your Toggle Name

<ToggleButton Name="tglBtn" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              BorderThickness="2" BorderBrush="Yellow" Background="White"/>

2) In trigger reference by the name given

<Trigger SourceName="tglBtn" Property="IsChecked" Value="True">
      <Setter TargetName="tglBtn" Property="Content" Value="different txt" />
</Trigger>

Hope that helps

Community
  • 1
  • 1
jmogera
  • 1,689
  • 3
  • 30
  • 65