MutliSelect Menu中的ListBox

时间:2016-07-15 17:49:56

标签: wpf vb.net listbox

我正在创建一个撤消按钮,它应该与visual studio撤消按钮相同。我希望用户能够将鼠标悬停在某个项目上,并选择其上方的每个项目。点击上面的所有项目都会传入并执行操作。如果用户单击按钮本身,请撤消顶部对象。问题:没有使用上下文菜单,也不知道我用wpf做了什么。现在用户可以鼠标悬停但只选择了鼠标在上面的对象(后面的所有代码都是vb.net)。感谢所有帮助!

代码:

<MenuItem Header="Undo" Name="MenuUndo" IsEnabled="True">
    <MenuItem.Icon>
        <Image Source="Undo.png" Width="24" Height="24" />
    </MenuItem.Icon>
    <StackPanel>
        <ListBox Name="ListBoxUndo" HorizontalAlignment="Stretch " 
                VerticalAlignment="Stretch" Width="177" Height="100 " Margin="-33,-5,-65,-5" 
                ScrollViewer.VerticalScrollBarVisibility="Visible" IsEnabled="True"
                SelectionMode ="Multiple">

            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="IsSelected" Value="True"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>

        </ListBox>                       
    </StackPanel>

</MenuItem>

目前的情况如下: enter image description here

1 个答案:

答案 0 :(得分:0)

嗯,我使用滚轮工作,无论如何都更好!这是我添加的代码:

 Private Sub ListBoxUndo_PreviewMouseWheel(sender As Object, e As MouseWheelEventArgs) Handles ListBoxUndo.PreviewMouseWheel
    ListBoxUndo.Focus()
    Try
        If e.Delta < 1 Then
            'highlight next item
            ListBoxUndo.SelectedItems.Add(ListBoxUndo.Items(ListBoxUndo.SelectedItems.Count))

        Else
            'remove last selected item
            ListBoxUndo.SelectedItems.Remove(ListBoxUndo.Items(ListBoxUndo.SelectedItems.Count - 1))
        End If
    Catch ex As Exception

    End Try

End Sub