C# - WPF - ComboBox -当鼠标悬停在焦点ComboBox上时防止滚动(使用附加属性)

2jcobegt  于 2023-05-01  发布在  C#
关注(0)|答案(1)|浏览(264)

后台

我需要防止一个ComboBox在悬停在它上面(当它被聚焦时)和使用鼠标滚轮滚动时滚动。
在这种情况下,我不能使用代码隐藏,所以我使用了一个附加属性。
我问这个问题只是为了将this question的答案从代码隐藏改为附加属性。

代码

下面是我的xaml

<ComboBox comboBox:NoScrollingBehaviour.Enabled="True"
          IsEditable="True" 
          IsTextSearchEnabled="True" 
          IsTextSearchCaseSensitive="False" 
          StaysOpenOnEdit="True"
          Text="{Binding MyTextProperty}"
          ItemsSource="{Binding DataContext.MySourceProperty, ElementName=MyElementName}"/>

下面是comboBox:NoScrollingBehaviour链接到的代码:

public static class NoScrollingBehaviour
{
    public static bool GetEnabled(ComboBox comboBox) =>
        (bool)comboBox.GetValue(EnabledProperty);

    public static void SetEnabled(ComboBox comboBox, bool value) =>
        comboBox.SetValue(EnabledProperty, value);

    public static readonly DependencyProperty EnabledProperty =
        DependencyProperty.RegisterAttached(
        "Enabled",
        typeof(bool),
        typeof(NoScrollingBehaviour),
        new UIPropertyMetadata(false, OnEnabledChanged));

    private static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ComboBox comboBox = (ComboBox)d;
        if (GetEnabled(comboBox))
        {
            FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
            stackPanel.AddHandler(FrameworkElement.RequestBringIntoViewEvent, new RequestBringIntoViewEventHandler(OnRequestBringIntoView));
            comboBox.ItemsPanel = new ItemsPanelTemplate() { VisualTree = stackPanel };
        }
    }

    private static void OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.Down) || Keyboard.IsKeyDown(Key.Up))
            return;

        e.Handled = true;
    }
}

What I have Tried

它只在 * 单击 * ComboBox时到达OnRequestBringIntoView,但在使用鼠标滚轮滚动时不会。
我还尝试为stackPanel添加MouseWheelEvent的事件,但似乎从未触发过。
然后,我尝试为comboBox添加一个事件,但只有当ComboBox * 未 * 聚焦时,使用鼠标滚轮滚动时才会触发该事件。

问题

如何防止ComboBox在对焦时滚动(使用鼠标滚轮)?

rfbsl7qr

rfbsl7qr1#

如果你只是想禁用鼠标滚轮的滚动功能,你可以在附加的行为中处理ComboBox本身的PreviewMouseWheel事件:

public static class NoScrollingBehaviour
{
    public static bool GetEnabled(ComboBox comboBox) =>
        (bool)comboBox.GetValue(EnabledProperty);

    public static void SetEnabled(ComboBox comboBox, bool value) =>
        comboBox.SetValue(EnabledProperty, value);

    public static readonly DependencyProperty EnabledProperty =
        DependencyProperty.RegisterAttached(
        "Enabled",
        typeof(bool),
        typeof(NoScrollingBehaviour),
        new UIPropertyMetadata(false, OnEnabledChanged));

    private static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ComboBox comboBox = (ComboBox)d;
        if (GetEnabled(comboBox))
            comboBox.PreviewMouseWheel += OnPreviewMouseWheel;
    }

    private static void OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        e.Handled = true;
    }
}

相关问题