You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据单选按钮选中状态开启/禁用ScrollViewer的输入绑定?

实现仅Cursor工具选中时启用ScrollViewer缩放输入绑定

下面提供几种实用的实现方案,根据你的项目架构选择即可:

方法1:通过命令CanExecute控制(MVVM推荐)

这种方式贴合MVVM设计模式,将工具选中状态与命令可用性绑定:

  1. 在ViewModel中添加工具选中状态属性:
private bool _isCursorToolSelected = true; // 默认选中Cursor工具
public bool IsCursorToolSelected
{
    get => _isCursorToolSelected;
    set
    {
        if (_isCursorToolSelected != value)
        {
            _isCursorToolSelected = value;
            OnPropertyChanged(nameof(IsCursorToolSelected));
            // 触发命令可用性更新
            ZoomInImageCommand.RaiseCanExecuteChanged();
            ZoomOutImageCommand.RaiseCanExecuteChanged();
        }
    }
}

// 假设使用RelayCommand实现命令
public ICommand ZoomInImageCommand { get; }
public ICommand ZoomOutImageCommand { get; }

// 命令初始化示例
public YourViewModel()
{
    ZoomInImageCommand = new RelayCommand(ExecuteZoomIn, CanZoom);
    ZoomOutImageCommand = new RelayCommand(ExecuteZoomOut, CanZoom);
}

// 共享的CanExecute逻辑
private bool CanZoom()
{
    return IsCursorToolSelected;
}

// 命令执行方法
private void ExecuteZoomIn()
{
    // 缩放逻辑
}

private void ExecuteZoomOut()
{
    // 缩放逻辑
}
  1. 将RadioButton的选中状态绑定到ViewModel属性:
<RadioButton x:Name="CursorTool" Style="{StaticResource {x:Type ToggleButton}}"
             IsChecked="{Binding IsCursorToolSelected}">
    <!-- 工具图标/文本 -->
</RadioButton>

当Cursor工具未选中时,命令的CanExecute返回false,对应的MouseBinding会自动失效,不会响应滚轮缩放操作。

方法2:直接绑定MouseBinding的IsEnabled属性

如果不想修改命令逻辑,可以直接将MouseBinding的启用状态绑定到CursorTool的选中状态:

<ScrollViewer>
    <ScrollViewer.InputBindings>
        <MouseBinding Gesture="{ext:MouseWheel Direction=Up, Keys=Control}"
                      Command="{Binding ZoomInImageCommand}"
                      IsEnabled="{Binding ElementName=CursorTool, Path=IsChecked}"/>
        <MouseBinding Gesture="{ext:MouseWheel Direction=Down, Keys=Control}"
                      Command="{Binding ZoomOutImageCommand}"
                      IsEnabled="{Binding ElementName=CursorTool, Path=IsChecked}"/>
    </ScrollViewer.InputBindings>
    <!-- 图片承载内容 -->
</ScrollViewer>

这种方式无需改动ViewModel,当CursorTool未选中时,MouseBinding的IsEnabled变为false,输入绑定就不会触发命令。

方法3:代码后台动态管理输入绑定(非MVVM场景)

如果使用传统代码后台开发,可以监听RadioButton的状态变化,动态添加/移除输入绑定:

public YourWindow()
{
    InitializeComponent();
    // 初始添加缩放绑定
    AddZoomBindings();
    // 绑定工具切换事件
    CursorTool.Checked += (s, e) => AddZoomBindings();
    CursorTool.Unchecked += (s, e) => RemoveZoomBindings();
}

private void AddZoomBindings()
{
    // 创建自定义MouseWheel手势(需匹配XAML中的ext:MouseWheel逻辑)
    var zoomInGesture = new MouseWheelGesture(MouseWheelDirection.Up, ModifierKeys.Control);
    var zoomInBinding = new MouseBinding(ZoomInImageCommand, zoomInGesture);
    
    var zoomOutGesture = new MouseWheelGesture(MouseWheelDirection.Down, ModifierKeys.Control);
    var zoomOutBinding = new MouseBinding(ZoomOutImageCommand, zoomOutGesture);
    
    ScrollViewer.InputBindings.Add(zoomInBinding);
    ScrollViewer.InputBindings.Add(zoomOutBinding);
}

private void RemoveZoomBindings()
{
    var targetBindings = ScrollViewer.InputBindings
        .OfType<MouseBinding>()
        .Where(b => b.Command == ZoomInImageCommand || b.Command == ZoomOutImageCommand)
        .ToList();
    
    foreach (var binding in targetBindings)
    {
        ScrollViewer.InputBindings.Remove(binding);
    }
}

这种方式耦合性较高,适合小型项目或快速原型开发。

内容的提问来源于stack exchange,提问作者mythter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 10:53:33