Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# scrollviewer中的组合框出现PageUp/PageDown问题_C#_.net_Wpf_Event Handling_Scroll - Fatal编程技术网

C# scrollviewer中的组合框出现PageUp/PageDown问题

C# scrollviewer中的组合框出现PageUp/PageDown问题,c#,.net,wpf,event-handling,scroll,C#,.net,Wpf,Event Handling,Scroll,我有一个简单的要求 我在scrollviewer中有一个组合框。当组合框打开,我按PageUp或PageDown时,如果垂直滚动条可见,则背景也会移动 是否有任何方法可以停止PageUp/PageDown,以便在必要时仅在组合框上工作 下面是Xaml和代码 <Window x:Class="WpfApplicationDemo.ComboInScrollViewer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/

我有一个简单的要求

我在scrollviewer中有一个组合框。当组合框打开,我按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");
         }
     }
 }
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
            }
        }
    }
}
使用System.Windows;
命名空间WpfApplicationDemo
{
/// 
///ComboInScrollViewer.xaml的交互逻辑
/// 
公共分部类ComboInScrollViewer:窗口
{
公共ComboInScrollViewer()
{
初始化组件();
listContainer.Items.Add(“第一”);
listContainer.Items.Add(“第二”);
listContainer.Items.Add(“第三”);
listContainer.Items.Add(“第四”);
listContainer.Items.Add(“第五”);
listContainer.Items.Add(“第六”);
}
}
}
我不能直接使用
e.handled=true
,因为事件首先为
ScrollViewer
触发,然后为
组合框
触发。我想我错过了什么。感谢您的帮助


谢谢:)

这对你有帮助吗

  <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;
    }
}

私有void UIElement_OnPreviewKeyDown(对象发送方,KeyEventArgs e)
{
if((e.Key==Key.PageUp | | e.Key==Key.PageDown)&&this.listContainer.isdropdown打开)
{
e、 已处理=正确;
}
}
您可以在其中放置一些其他自定义逻辑,而不是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;

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");
         }
     }
 }
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
            }
        }
    }
}
使用System.Windows;
使用System.Runtime.InteropServices;
使用制度;
使用System.Windows.Input;
命名空间WpfApplicationDemo
{
/// 
///ComboInScrollViewer.xaml的交互逻辑
/// 
公共分部类ComboInScrollViewer:窗口
{
[DllImport(“user32.dll”,CharSet=CharSet.Auto,ExactSpelling=true)]
公共静态外部IntPtr GetActiveWindow();
公共ComboInScrollViewer()
{
初始化组件();
listContainer.Items.Add(“第一”);
listContainer.Items.Add(“第二”);
listContainer.Items.Add(“第三”);
listContainer.Items.Add(“第四”);
listContainer.Items.Add(“第五”);
listContainer.Items.Add(“第六”);
}
private void ScrollViewer_PreviewKeyDown(对象发送方,System.Windows.Input.KeyEventArgs e)
{
if(e.Key==Key.PageUp | e.Key==Key.Prior)
{
listContainer.RaiseEvent(新的KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice,PresentationSource.FromVisual)(listContainer),
1,Key.Home){RoutedEvent=Keyboard.KeyDownEvent}
);
e、 已处理=正确;
}
else if(e.Key==Key.PageDown | | e.Key==Key.Next)
{
listContainer.RaiseEvent(新的KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice,PresentationSource.FromVisual)(listContainer),
1,Key.End){RoutedEvent=Keyboard.KeyDownEvent}
);
e、 已处理=正确;
}
其他的
{
e、 已处理=错误;
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
尝试
{
listContainer.RaiseEvent(新的KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice,PresentationSource.FromVisual)(listContainer),
1,Key.End){RoutedEvent=Keyboard.KeyDownEvent}
);
}
捕获(异常objExp)
{
//处理错误
}
}
}
}

如果我这样做,scrollviewer将不会滚动,并且事件也不会在组合框中工作。啊,好的,scrollviewer将工作,它将仅在组合框打开时停止工作。您将需要EventManager和注册一个处理程序,该处理程序甚至可以调用方法,即使其handled设置为true。我将尝试找到一个例子。谢谢dev。如果你找到任何例子,请让我知道。