Why won't IsChecked change on this toggle button?
Asked Answered
G

3

5

I have the following xaml for a toggle button:

<ToggleButton Margin="0,3" Grid.Row="3" Grid.ColumnSpan="2" Command="{Binding DataContext.SelectAllCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
  <ToggleButton.Style>
   <Style TargetType="{x:Type ToggleButton}">
       <Setter Property="Content" Value="Select All"/>
       <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Content" Value="Select None"/>
                <Setter Property="Command" Value="{Binding DataContext.SelectNoneCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
            </Trigger>
          </Style.Triggers>
      </Style>
  </ToggleButton.Style>
</ToggleButton>

But the IsChecked property never gets updated when clicking the button.

If I use snoop and force the IsChecked to True then the trigger kicks in.

Gressorial answered 6/1, 2011 at 17:47 Comment(1)
What does your Command do? If I remove your Commands (and just set the Content properties, it works fine.Kelwunn
V
6

I tried your ToggleButton and it's working fine. The only problem I see with it is that you set Command explictly. It should be done with a Setter instead (like you did with Content) to not break the Trigger.

<ToggleButton Name="toggleButton" Margin="0,3" Grid.Row="3" Grid.ColumnSpan="2">
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Content" Value="Select All"/>
            <Setter Property="Command"
                    Value="{Binding DataContext.SelectAllCommand,
                                    Mode=OneWay,
                                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="Select None"/>
                    <Setter Property="Command" Value="{Binding DataContext.SelectNoneCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

If you're still unable to Click it, I'd check if IsHitTestVisible or similar is set to False further up the Visual Tree.

If you want to compare your version to my test app to see what's not working, I uploaded it here: http://www.mediafire.com/?ik24ftsfw2wwfwb

Valet answered 6/1, 2011 at 18:57 Comment(1)
argh, cant access - company firewall blocked it! Thanks for your help thoughGressorial
G
3

Fixed it by parameterising my command, binding to a new property on my viewmodel and moving Command into the style:

               <ToggleButton Margin="0,3" Grid.Row="3" Grid.ColumnSpan="2" IsThreeState="False" 
                            IsChecked="{Binding DataContext.IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                    <ToggleButton.Style>
                        <Style TargetType="{x:Type ToggleButton}">
                            <Setter Property="Content" Value="Select All"/>
                            <Setter Property="Command" Value="{Binding DataContext.SelectAllCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>                                
                            <Style.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Content" Value="Select None"/>                                
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>

Don't you just love WPF sometimes!

Gressorial answered 6/1, 2011 at 19:7 Comment(2)
WPF IS AWESOME!Leila
WPF is the ex-wife.Quadrillion
E
1

Just a guess, but remove the Binding Mode=OneWay and try again.

Exposure answered 6/1, 2011 at 17:51 Comment(1)
thanks but no luck.. Do I need to bind something to the IsChecked property to get this working?Gressorial

© 2022 - 2024 — McMap. All rights reserved.