64

我有一个组合框,我想防止用户使用鼠标滚轮滚动项目。

有没有简单的方法可以做到这一点?

(C#,VS2008)

4

4 回答 4

119

为您的 ComboBox使用MouseWheel事件:

void comboBox1_MouseWheel(object sender, MouseEventArgs e) {
    ((HandledMouseEventArgs)e).Handled = true;
}

注意:您必须在代码中创建事件:

comboBox1.MouseWheel += new MouseEventHandler(comboBox1_MouseWheel);
于 2009-12-10T18:34:10.040 回答
5

对于 WPF,请PreviewMouseWheel改为处理事件。

考虑一下也是一个好主意,ComboBox.IsDropDownOpen因此如果在展开时选择中有很多项目,用户仍然可以使用鼠标滚动ComboBox

另一件事是在整个应用程序中应用相同的行为。

我通常使用以下代码执行上述所有操作:

应用程序.xaml

<Application.Resources>
    <Style TargetType="ComboBox">
        <EventSetter Event="PreviewMouseWheel" Handler="ComboBox_PreviewMouseWheel" />
    </Style>
</Application.Resources>

应用程序.xaml.cs

private void ComboBox_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
    e.Handled = !((System.Windows.Controls.ComboBox)sender).IsDropDownOpen;
}
于 2018-08-29T16:24:29.813 回答
0

我的组合框被放置在 DataGrid [C#, WPF XAML] 中,就像这样:

    <DataGrid x:Name="dgvFieldsMapping" Grid.Row="1" ItemsSource="{Binding}">
        <DataGrid.Columns>
            ...
            <DataGridTemplateColumn Width="*" Header="Destination Field" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate >
                        <ComboBox ItemsSource="{Binding Source={StaticResource CustomerDbFields}}" SelectedValue="{Binding destinationField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            ...
        </DataGrid.Columns>
    </DataGrid>

So whenever a DropDown was closed after selecting a Value, the Mousewheel would scroll that Combobox's Items and modify my Selection.

我最终将我的 XAML 修改为如下所示:

    <DataGrid x:Name="dgvFieldsMapping" Grid.Row="1" ItemsSource="{Binding}">
        <DataGrid.Resources>
            <Style x:Key="dgvComboBox_Loaded" TargetType="ComboBox">
                <EventSetter Event="Loaded" Handler="dgvCombobox_Loaded" />
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            ...
            <DataGridTemplateColumn Width="*" Header="Destination Field" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate >
                        <ComboBox Style="{StaticResource dgvComboBox_Loaded}" ItemsSource="{Binding Source={StaticResource CustomerDbFields}}" SelectedValue="{Binding destinationField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            ...
        </DataGrid.Columns>
    </DataGrid>

并在代码隐藏中添加这些行

        public void dgvCombobox_Loaded(Object sender, RoutedEventArgs e)
        {
            ((ComboBox)sender).DropDownClosed -= ComboBox_OnDropDownClosed;
            ((ComboBox)sender).DropDownClosed += new System.EventHandler(ComboBox_OnDropDownClosed);
        }

        void ComboBox_OnDropDownClosed(object sender, System.EventArgs e)
        {
            dgvFieldsMapping.Focus();
        }

通过这种方式,我只是在关闭其相应的 DropDown 后将焦点从 ComboBox 移到外部 DataGrid,所以我不需要添加任何虚拟 FrameWorkElement

于 2019-10-10T15:28:03.927 回答
0

我使用另一种也适用于 Mono 的解决方案。

目标是防止意外滚动(即当用户在使用鼠标滚轮时没有查看组合框时)。如果他/她在 comboBox 的可见部分之外滚动,则组合框不应该滚动,否则应该滚动。

我的解决方案:

  • 在屏幕的可见部分之外放置一个只读文本框。在 form_load 我放置了一行: hiddenTextbox.left = -100 ;

  • 当鼠标离开组合框时使用鼠标离开事件将焦点设置到此文本框。在 comboBox1_MouseLeave 我放置了一行: hiddenTextbox.focus();

  • 处理 mouseWheel 事件:From1.MouseWheel += Form1_MouseWheel;textBoxHidden.MouseWheel += Form1_MouseWheel;

于 2015-10-08T07:21:14.643 回答