pageup/pageDown问题在滚动浏览器中的组合时



我有一个简单的要求。

我在滚动浏览器中有一个组合。当Combobox打开并按Pageup或patedown,如果可见垂直滚动条,背景也在移动。

是否有任何方法可以在必要时阻止Pageup/pageDown在Combobox上工作?

以下是XAML和代码behind。

<Window x:Class="WpfApplicationDemo.ComboInScrollViewer"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Title="ComboInScrollViewer" Height="300" Width="300">
     <ScrollViewer>
         <Grid Height="500">
             <ComboBox x:Name="listContainer" Height="30" Width="90" />
         </Grid>
     </ScrollViewer>
 </Window>

CodeBehind

using System.Windows;
namespace WpfApplicationDemo
 {
     /// <summary>
     /// Interaction logic for ComboInScrollViewer.xaml
     /// </summary>
     public partial class ComboInScrollViewer : Window
     {
         public ComboInScrollViewer()
         {
             InitializeComponent();
             listContainer.Items.Add("1st");
             listContainer.Items.Add("2nd");
             listContainer.Items.Add("3rd");
             listContainer.Items.Add("4th");
             listContainer.Items.Add("5th");
             listContainer.Items.Add("6th");
         }
     }
 }

我不能直接使用e.handled = true,因为该事件是首先针对ScrollViewer发射的,然后将其触发给Combobox。我想我缺少一些东西。任何帮助都将受到赞赏。

谢谢:)

这将帮助您任何futher?

  <ScrollViewer PreviewKeyDown="UIElement_OnPreviewKeyDown">
    <Grid Height="500">
      <ComboBox x:Name="listContainer" Height="30" Width="90"/>
    </Grid>
  </ScrollViewer>

private void UIElement_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.PageUp || e.Key == Key.PageDown) && this.listContainer.IsDropDownOpen)
    {
        e.Handled = true;
    }
}

而不是e.handled,您可以在内部放置其他自定义逻辑。

只是一个想法:)

尝试了1-2个月后,这是一项工作

XAML

<Window x:Class="WpfApplicationDemo.ComboInScrollViewer"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ComboInScrollViewer" Height="300" Width="300">
    <ScrollViewer x:Name="scParent" PreviewKeyDown="ScrollViewer_PreviewKeyDown" >
        <Grid Height="500">
            <ComboBox x:Name="listContainer" Height="30" Width="90" />
            <Button Click="Button_Click" Height="30" Width="90" VerticalAlignment="Top" Margin="0,100,0,0"/>
            <TextBox x:Name="txtValue" Width="90" VerticalAlignment="Top" Margin="0,50,0,0"/>
        </Grid>
    </ScrollViewer>
</Window>

代码

using System.Windows;
using System.Runtime.InteropServices;
using System;
using System.Windows.Input;
namespace WpfApplicationDemo
{
    /// <summary>
    /// Interaction logic for ComboInScrollViewer.xaml
    /// </summary>
    public partial class ComboInScrollViewer : Window
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetActiveWindow();
        public ComboInScrollViewer()
        {
            InitializeComponent();
            listContainer.Items.Add("1st");
            listContainer.Items.Add("2nd");
            listContainer.Items.Add("3rd");
            listContainer.Items.Add("4th");
            listContainer.Items.Add("5th");
            listContainer.Items.Add("6th");
        }
        private void ScrollViewer_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.PageUp || e.Key == Key.Prior)
            {
                listContainer.RaiseEvent(new KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice, PresentationSource.FromVisual(listContainer),
                    1, Key.Home) { RoutedEvent = Keyboard.KeyDownEvent }
                );
                e.Handled = true;
            }
            else if (e.Key == Key.PageDown || e.Key == Key.Next)
            {
                listContainer.RaiseEvent(new KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice, PresentationSource.FromVisual(listContainer),
                    1, Key.End) { RoutedEvent = Keyboard.KeyDownEvent }
                );
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                listContainer.RaiseEvent(new KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice, PresentationSource.FromVisual(listContainer),
                    1, Key.End) { RoutedEvent = Keyboard.KeyDownEvent } 
                );
            }
            catch (Exception objExp)
            {
                // Handle Error
            }
        }
    }
}

最新更新