【问题标题】:Mousewheel scrolling on a Listbox within a Ribbon [WPF]鼠标滚轮在功能区中的列表框上滚动 [WPF]
【发布时间】:2018-06-18 18:34:16
【问题描述】:

我有一个 带有两个选项卡的功能区,其中一个选项卡包含一个列表框,其中包含自动生成的内容。

我希望鼠标滚轮可以滚动列表框内容,并尝试了很多方法,但鼠标滚轮只能切换功能区选项卡。

以下是我尝试过的一种解决方案,我尝试从功能区释放鼠标控件并将其提供给 ListBox,但到目前为止我还没有成功。有没有合适的方法来做到这一点?我错过了什么吗?

xml

<Ribbon Name="ribbonMain">
   <RibbonTab Header="Home">
      <RibbonGroup Header="Employees" Width="200">
         <ListBox x:Name="empListBox" ItemsSource="{Binding SelectedEmployees}" 
             Width="180" 
             MouseEnter="empListBox_MouseEnter" 
             MouseLeave="empListBox_MouseLeave">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Path=FirstName}" IsChecked="{Binding IsChecked}"></CheckBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
          </ListBox>
     </RibbonGroup>

xaml.cs

 private void empListBox_MouseEnter(object sender, MouseEventArgs e)
    {
        if (sender is ListBox)
        {
            ribbonMain.ReleaseMouseCapture();
            ((ListBox)sender).CaptureMouse();
        }
    }

    private void empListBox_MouseLeave(object sender, MouseEventArgs e)
    {
        ((ListBox)sender).ReleaseMouseCapture();
        ribbonMain.CaptureMouse();
    }

【问题讨论】:

    标签: c# wpf xaml listbox mousewheel


    【解决方案1】:

    我认为问题在于ListBox 本身不能真正对鼠标捕获做任何事情。我认为实际上是滚动查看器ListBox 模板中需要接收滚轮事件。

    尝试像这样更改鼠标处理程序:

    private void empListBox_MouseEnter(object sender, MouseEventArgs e)
    {
        Mouse.Capture(empListBox, CaptureMode.SubTree);
    }
    
    private void empListBox_MouseLeave(object sender, MouseEventArgs e)
    {
        empListBox.ReleaseMouseCapture();
    }
    

    这将使鼠标事件对持有捕获的元素可见 *和* 其后代元素,包括滚动查看器。

    【讨论】:

    • 很有趣,很高兴知道以供将来参考。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多