更新:2007 年 11 月
本範例示範如何在滑鼠指標移入及移出項目佔據的區域時,變更該項目的色彩。
本範例包含一個可延伸標記語言 (XAML) 檔案和一個程式碼後置 (Code-Behind) 的檔案。如需完整範例,請參閱滑鼠指標範例。
注意事項: |
|---|
範例
下列 XAML 會建立使用者介面 (由 TextBlock 周圍的 Border 組成),並將 MouseEnter 和 MouseLeave 事件處理常式 (Event Handler) 附加至 Border。
<StackPanel>
<Border MouseEnter="OnMouseEnterHandler"
MouseLeave="OnMouseLeaveHandler"
Name="border1" Margin="10"
BorderThickness="1"
BorderBrush="Black"
VerticalAlignment="Center"
Width="300" Height="100">
<Label Margin="10" FontSize="14"
HorizontalAlignment="Center">Move Cursor Over Me</Label>
</Border>
</StackPanel>
下列程式碼後置會建立 MouseEnter 和 MouseLeave 事件處理常式。當滑鼠指標移入 Border 時,Border 的背景會變成紅色。當滑鼠指標移出 Border 時,Border 的背景就會變回白色。
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
' raised when mouse cursor enters the are occupied by the element
Private Sub OnMouseEnterHandler(ByVal sender As Object, ByVal e As MouseEventArgs)
border1.Background = Brushes.Red
End Sub
' raised when mouse cursor leaves the are occupied by the element
Private Sub OnMouseLeaveHandler(ByVal sender As Object, ByVal e As MouseEventArgs)
border1.Background = Brushes.White
End Sub
End Class
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
// raised when mouse cursor enters the area occupied by the element
void OnMouseEnterHandler(object sender, MouseEventArgs e)
{
border1.Background = Brushes.Red;
}
// raised when mouse cursor leaves the area occupied by the element
void OnMouseLeaveHandler(object sender, MouseEventArgs e)
{
border1.Background = Brushes.White;
}
}
注意事項: