Can someone please help me out with this? I have the following template setup in WPF:
<Style TargetType="{x:Type Label}" x:Key="NavLink">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="NavLinkControlTemplate" TargetType="{x:Type Label}">
<Border x:Name="NavLinkBorder">
<ContentPresenter x:Name="NavLinkContent" Margin="4,4,4,4" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="NavLinkBorder" Property="Background" Value="#CCCCCC" />
<Setter Property="Cursor" Value="Hand" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="NavLinkBorder" Property="Background" Value="#EAEAEA" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
When my program is loaded, it automatically creates a list of labels that serve as the navigation menu. As you can see from the above, when the mouse hovers over one of the labels the background color is changed. The only problem with this is that I also have a context menu applied to the labels, and when I right-click to bring it up the label background goes back to its original color instead of staying the MouseOver color.
I've Googled for about an hour or so and can't seem to find a Trigger Property that will check if the right mouse button is pressed, so I'm assuming there isn't one. I'm thinking maybe I can accomplish this via code.
I have tried the following code, but I'm not having any luck:
// this event is being added to each label at runtime...
tempLabel.MouseRightButtonUp += new MouseButtonEventHandler(NavLink_RightClicked);
// this is the method that the right-click calls...
private void NavLink_RightClicked(object sender, EventArgs e)
{
if (sender is Label)
{
currentContextLink = sender as Label;
// the below line won't work because the ControlTemplate seems to be overwriting it...
currentContextLink.Background = new SolidColorBrush(appFunctions.HexToColor("#FF0000"));
}
}
I also tried getting the Label's parent element as a Border, but it seems that since it's setup through a template, the parent of the Label is actually the StackPanel I have containing all the labels.
Can someone please help me figure out how to access the border and change its Background color, or maybe guide me in any direction that may help me accomplish this?
Any help is GREATLY appreciated!