简体   繁体   English

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

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

I was wondering if there is any relatively simple way to essentially create an 'undo' button that would undo whatever event happened right before it. 我想知道是否有相对简单的方法来本质上创建一个“撤消”按钮,该按钮可以撤消发生在它之前的任何事件。 The problem is, I can't do it explicitly (for example, if the background was white, and then it became red, I can't just have the undo button reset the background as white). 问题是,我不能明确地执行此操作(例如,如果背景为白色,然后变成红色,则不能仅使撤消按钮将背景重置为白色)。 I can't do it this way because I won't know which event happened last, there could be a number of events that could have happened, and I don't want an individual undo button for every single event. 我无法以这种方式进行操作,因为我不知道最后一个事件发生了,可能会发生许多事件,并且我不想为每个事件都使用单独的撤消按钮。

To provide an example, I have a few labels in a grid, and when I mouse over any label it changes to a bigger size, and all the other labels become a standard (smaller) size. 举个例子,我在网格中有几个标签,当我将鼠标悬停在任何标签上时,它会更改为更大的尺寸,所有其他标签都变为标准(较小)的尺寸。 However, sometimes one of the labels will already be a bigger size (from a button, or such) -- lets call this label1. 但是,有时其中一个标签的大小已经很大(从按钮等开始),我们称其为label1。 So when I mouse over a different label -- lets call this label2 -- then label2 becomes bigger whereas label1 is small now. 因此,当我将鼠标悬停在另一个标签上时-将此标签称为-标签2会变大,而标签1现在会变小。 But when I move the mouse off of label2, I want label1 to be big again whereas label2 should be small again. 但是,当我将鼠标移离label2时,我希望label1再次变大,而label2应该再次变小。 Thanks for any help/ strategies in advance!! 感谢您的任何帮助/预先的策略!

PS I'm pretty new to WPF so the simpler the solution the better :) But anything is appreciated! PS我对WPF来说还很陌生,因此解决方案越简单越好:)但是,任何事情都值得赞赏!

EDIT: I think an easy way of asking this is: Is there any way to create a MouseLeave event that undoes whatever was done by a MouseEnter event? 编辑:我认为一个简单的方法是:有没有办法创建一个MouseLeave事件来撤消MouseEnter事件的作用?

I don't do VB so sorry for conventions, but this should work for you as a quick example: 我不做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>

code behind: 后面的代码:

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.

相关问题 ShowDialog 允许在 WPF VB.NET 下方的 MainWindow 上触发事件 - ShowDialog Allowing Event Triggers on MainWindow Underneath WPF VB.NET WPF VB.NET 中的经过时间事件处理程序 - Elapsed Time Event Handler in WPF VB.NET 在WPF中动态创建表(VB.Net) - Create table dynamically in WPF (VB.Net) WPF使用Vb.net创建矩形 - WPF create Retangles using Vb.net 如果在 VB.NET 的 WPF MVVM 中触发 KeyDown 事件,执行命令的最简单方法是什么? - Easiest way to execute a command if KeyDown-event is fired in WPF MVVM with VB.NET? 如何设置图像淡入/淡出wpf vb.net(使用xaml代码和vbcode)并将其作为用户控件添加到winforms中 - How do you set fade in/out wpf vb.net for images(using xaml code and vbcode) and add it to winforms as usercontrol WPF vb.net单击页面上的事件后,启用窗口上的按钮 - WPF vb.net Make buttons on Window enable after click event on Page VB.NET中的WPF中的Observable Collection发生更改时,不会触发INotifyPropertyChanged事件 - INotifyPropertyChanged event not firing when Observable Collection changed in WPF in VB.NET 如何在VB.Net 3.0中声明依赖属性 - How Do you Declare a Dependancy Property in VB.Net 3.0 如何在vb.net,WPF中截取屏幕截图? - How to take screenshots in vb.net, WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM