I have a Label control with context menu and I want to change it's background color when the mouse enters it's rectangle and return it's parent background color when the mouse leaves.
Changing background is implemented inside MouseEnter and MouseLeave event handlers.
The problem is when I click the right button and the context menu shows up the MouseLeave event is raised and control's background is changed back to it's parent background but I want it to stay the same.
Here is the code:
//Label controls are created dynamically and added to a StackPanel
ContextMenu contextMenu = new ContextMenu();
contextMenu.Items.Add("MenuItem");
Label deviceLabel = new Label()
{
Content = "LabelText",
ContextMenu = contextMenu
};
//when mouse enters label bounds change it's background to AliceBlue color
deviceLabel.MouseEnter += delegate
{
deviceLabel.Background = new SolidColorBrush(Colors.AliceBlue);
};
//when mouse leaves label bounds get it's parent panel control background color back
deviceLabel.MouseLeave += delegate
{
deviceLabel.Background = (Brush)this.Parent.GetValue(Panel.BackgroundProperty);
};
stackPanel.Children.Add(deviceLabel);
So how can I leave label background with AliceBlue color while it's context menu is open?