[英]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.