在scrollviewer中组合框时出现PageUp / PageDown问题

时间:2014-02-04 04:29:21

标签: c# .net wpf event-handling scroll

我有一个简单的要求。

我在滚动查看器中有一个Combobox。当组合框打开并按下PageUp或PageDown时,如果垂直滚动条可见,背景也会移动。

有必要阻止PageUp / PageDown仅在组合框上工作吗?

下面是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>
         <Grid Height="500">
             <ComboBox x:Name="listContainer" Height="30" Width="90" />
         </Grid>
     </ScrollViewer>
 </Window>

代码隐藏

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触发事件。我想我错过了什么。任何帮助表示赞赏。

谢谢:)

2 个答案:

答案 0 :(得分:1)

这会对你有什么帮助吗?

  <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 :(得分:1)

尝试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
            }
        }
    }
}