Question

Is there anyway to remove the behavior of setting IsChecked attribute of a ToggleButton when I click the toggle button? I want the IsChecked attribute to be handled by binding to a custom property.

Was it helpful?

Solution

So you want the control to only handle its IsChecked state via your binding and disallow input from the user. You could set IsEnabled="False" but then it will appear a bit opaque in theDisabled` state unless you alter the control template accordingly.

I think what you want would be more of a way to just omit the user input but still handle your IsChecked so I would suggesting setting it like;

<ToggleButton IsHitTestVisible="False" IsTabStop="False" IsChecked="{Binding blah}"/>

Hope this helps, cheers.

OTHER TIPS

Nice solution Chris W. Thank you! If someone still want to be able to check by clicking a button but prevent unchecking from the UI:

<Style TargetType="ToggleButton" x:Key="MyToggle">
  <Style.Triggers>
    <Trigger Property="IsChecked" Value="True">
      <!-- Prevents unchecking -->
      <Setter Property="IsHitTestVisible" Value="False"/>
      <Setter Property="IsTabStop" Value="False"/>
    </Trigger>
  </Style.Triggers>
</Style>

I know this question is pretty old but I think my answer could help someone.

So my problem is related to navigation toggle buttons. I have set IsChecked property to true in my code and the related view is showing but the design of ToggleButton was changing when user pressed it (while the view was staying the same). So eventually I had the view showing but ToggleButton was unchecked and IsChecked property behind was set to true. Totally insane.

To deal with this I made my own very simple custom control:

class CustomToggleButton : ToggleButton
{
    protected override void OnToggle()
    {
        if (IsChecked != true)
        {
            base.OnToggle();
        }
    }
}

Now ToggleButton is unchecking only from my code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top