我想知道是否有任何相对简单的方法来基本上创建一个“撤消”按钮,可以撤消之前发生的任何事件。问题是,我不能明确地做到这一点(例如,如果背景是白色的,然后它变成红色,我不能让撤消按钮将背景重置为白色)。我不能这样做,因为我不知道最后发生了哪个事件,可能发生了许多事件,而且我不希望每个事件都有一个单独的撤销按钮。
举一个例子,我在网格中有一些标签,当我将鼠标悬停在任何标签上时,它会变为更大的尺寸,而所有其他标签变成标准(更小)的尺寸。但是,有时其中一个标签已经是一个更大的尺寸(从一个按钮,或类似) - 让我们调用这个标签1。因此,当我将鼠标悬停在另一个标签上时 - 让我们调用此标签2 - 然后label2变大,而label1现在变小。但是当我将标签移出label2时,我希望label1再次变大,而label2应该再次变小。感谢提前任何帮助/策略!!
P.S。我对WPF很新,所以解决方案越简单越好:)但是有什么值得赞赏的!
编辑:我认为一个简单的问题是:有没有办法创建一个MouseLeave事件来撤消MouseEnter事件所做的任何事情?
答案 0 :(得分:0)
我不做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