Search code examples
c#.netwpfmvvm

How to "turn on and off" bindings of an element when toggle button or radio button is checked or unchecked


I make a simple image editor and I have a scrollviewer element where I put a picture. Scrollviewer has some bindings to zoom in and out a picture:

<ScrollViewer>
    <ScrollViewer.InputBindings>
        <MouseBinding Gesture="{ext:MouseWheel Direction=Up, Keys=Control}" Command="{Binding ZoomInImageCommand}"/>
        <MouseBinding Gesture="{ext:MouseWheel Direction=Down, Keys=Control}" Command="{Binding ZoomOutImageCommand}"/>
    </ScrollViewer.InputBindings>
    ...
</ScrollViewer>

On a toolbar there are some tools like Cursor (do nothing, just move around the image), Crop, Rotate etc.

<ToolBar>
    <RadioButton x:Name="CursorTool" Style="{StaticResource {x:Type ToggleButton}}">
        ...                         
    </RadioButton>
    <RadioButton x:Name="CropTool" Style="{StaticResource {x:Type ToggleButton}}">
        ...                         
    </RadioButton>
    ...
</ToolBar>

So I need the bindings of a scrollviewer works only when Cursor tool is checked. How it can be done?


Solution

  • You just bind the property IsChecked of CursorTool to IsHitTestVisible of parent (here ScrollViewer):

    <ScrollViewer IsHitTestVisible="{Binding ElementName=CursorTool, Path=IsChecked}"
    

    When you set false to IsHitTestVisible, all mouse activities are stopped on whole container, so you cant scrolling