Reputation: 403
I am trying to change the color of a label's text when I hover over it. I tried putting the command in the previewmousemove event but this doesn't work.
private void hand_PreviewMouseMove(object sender, PreviewMouseEventArgs e)
{
Cursor.Current = Cursors.Hand;
xrLabel260.BackColor = Color.CornflowerBlue;
}
After this did not work, I tried to use the mouseenter/mouseleave events to change the color.
private void xrLabel260_MouseEnter(object sender, EventArgs e)
{
xrLabel260.ForeColor = Color.CornflowerBlue;
}
private void xrLabel260_MouseLeave(object sender, EventArgs e)
{
xrLabel260.ForeColor = Color.Black;
}
This did not work either. How could I change my code so that it will work? Thank you in advance for your help.
Upvotes: 0
Views: 8119
Reputation: 880
Personally I would do something like this in your xaml: EDIT: I have modified this to show how it fits into a main window.
<Window x:Class="WpfApplication1.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">
<Grid>
<Label Content="My Label">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</Grid>
Upvotes: 1
Reputation: 1449
It seems like you didn't add event handler (register mouse events for label) for it:
xrLabel260.MouseEnter += xrLabel260_MouseEnter;
Most logical place to do it is in form's load routine.
Edit: for WPF you can have something like this in XAML (question had EventArgs instead of MouseEventArgs, I thought it was for WinForms):
<Label x:Name="xrLabel260" Content="Label" MouseEnter="xrLabel260_MouseEnter"/>
...and then in code-behind:
private void xrLabel260_MouseEnter(object sender, MouseEventArgs e)
{
xrLabel260.Foreground = Brushes.BlanchedAlmond;
}
Upvotes: 1