我不做 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