Gerard
Gerard

Reputation: 13397

Command of ToggleButton does not fire when using a style

The following ToggleButton works as expected:

<ToggleButton Command="{Binding ToggleCommand}"
              RenderOptions.BitmapScalingMode="HighQuality"
              ToolTip=".." 
              VerticalAlignment="Stretch"
              Focusable="False" IsChecked="False" Margin="2" Padding="0"
              Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
     <Image Source="/..;component/Resources/...png" Height="26"/>
</ToggleButton>

When clicked the implementation of ToggleCommand is executed.

Then I decided to use a style like this:

<Style x:Key="ButtonToggle" TargetType="ToggleButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <ToggleButton RenderOptions.BitmapScalingMode="HighQuality"
                              VerticalAlignment="Stretch"
                              Focusable="False" IsChecked="False" Margin="2" Padding="0"
                              Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
                    <Image Source="/..;component/Resources/...png" Height="26"/>
                </ToggleButton>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Together with:

<ToggleButton DockPanel.Dock="Right" Command="{Binding ToggleCommand}" ToolTip=".."
              Style="{StaticResource ButtonToggle}" />

The display is ok.
The binding with ToggleCommand is ok.
Just when I click the button the implementation of ToggleCommand is not executed.
Can anybody explain why that is?

Upvotes: 1

Views: 411

Answers (1)

ZSH
ZSH

Reputation: 913

you need to bind the command of the control in the controlTemplate
add Command="{TemplateBinding Command}" in the togglebutton within the style as:

            <Style x:Key="ButtonToggle" TargetType="ToggleButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <ToggleButton RenderOptions.BitmapScalingMode="HighQuality"
                          Command="{TemplateBinding Command}"
                          VerticalAlignment="Stretch"
                          Focusable="False" IsChecked="False" Margin="2" Padding="0"
                          Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
                            <Image Source="/..;component/Resources/...png" Height="26"/>
                        </ToggleButton>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Upvotes: 1

Related Questions