繁体   English   中英

如何使用VB.NET创建事件以撤消WPF中的上一个事件

[英]How do you create an event to undo the previous event in WPF with VB.NET

我想知道是否有相对简单的方法来本质上创建一个“撤消”按钮,该按钮可以撤消发生在它之前的任何事件。 问题是,我不能明确地执行此操作(例如,如果背景为白色,然后变成红色,则不能仅使撤消按钮将背景重置为白色)。 我无法以这种方式进行操作,因为我不知道最后一个事件发生了,可能会发生许多事件,并且我不想为每个事件都使用单独的撤消按钮。

举个例子,我在网格中有几个标签,当我将鼠标悬停在任何标签上时,它会更改为更大的尺寸,所有其他标签都变为标准(较小)的尺寸。 但是,有时其中一个标签的大小已经很大(从按钮等开始),我们称其为label1。 因此,当我将鼠标悬停在另一个标签上时-将此标签称为-标签2会变大,而标签1现在会变小。 但是,当我将鼠标移离label2时,我希望label1再次变大,而label2应该再次变小。 感谢您的任何帮助/预先的策略!

PS我对WPF来说还很陌生,因此解决方案越简单越好:)但是,任何事情都值得赞赏!

编辑:我认为一个简单的方法是:有没有办法创建一个MouseLeave事件来撤消MouseEnter事件的作用?

我不做VB,所以对不起约定,但这是一个适合您的快速示例:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel x:Name="stackPanel">
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >1</Label>
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >2</Label>
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >3</Label>
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >4</Label>
     </StackPanel>
 </Window>

后面的代码:

Class MainWindow

Dim _mouseLeaveSize As Double = 10
Dim _mouseEnterSize As Double = 20

Private Sub OnMouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)

    For Each child As Visual In stackPanel.Children
        SetLabelLeaveProperties(child)
    Next

    Dim label = CType(sender, Label)
    label.FontSize = _mouseEnterSize

End Sub

Private Sub SetLabelLeaveProperties(ByVal myVisual As Visual)

    Dim label = TryCast(myVisual, Label)

    If label Is Nothing Then
        'iterate thru children to see if anymore labels 
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(myVisual) - 1
            Dim child = VisualTreeHelper.GetChild(myVisual, i)
            Dim l = TryCast(child, Label)
            If l Is Nothing Then
                SetLabelLeaveProperties(child)  'Enumerate children of the child visual object.
            Else
                l.FontSize = _mouseLeaveSize
            End If
        Next i
    Else
        label.FontSize = _mouseLeaveSize
    End If

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM