I'm pretty new to WPF -- tantalized by the possibilities, but getting frustrated.... I'm trying to make it so different tabs on a TabControl have different foregound/background colors according to when they're selected. First thing I did was to create new TabItem class called PlayerTabItem and gave it a Brush SelectedBackground property. This is a music player app, so "player" in this context refers to its use in this app. (I first created SelectedBackground as a simple property, then later as a dependency property, but this didn't seem to change anything, so I have omitted that implementation.)
class PlayerTabItem : TabItem
{
public Brush SelectedBackground { get; set; }
}
Then I used this is in my XAML, and it compiled okay (so long as I put "local:" in front of the tag name), and recognized the new property I created. Of course, the property didn't do anything. This is where I got stuck.
<local:PlayerTabItem Header="Now Playing" SelectedBackground="Blue"/>
<local:PlayerTabItem Header="Collection" SelectedBackground="Purple"/>
<local:PlayerTabItem Header="Search" SelectedBackground="Green"/>
I tried handling selection events in the PlayerTabItem class in order to apply the background color, but this was a dead end. (Overriding PlayerTabItem.OnSelected and setting color there had no effect -- ran without error but did nothing.) Then I tried adding a Style with a new ControlTemplate and a Trigger for IsSelected = true, and I started getting closer....it worked if I just hard-coded a color in the Trigger.Setter:
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="Purple" />
</Trigger>
But what I really want is to bind to the PlayerTabItem's SelectedBackground color. I tried this:
<Setter TargetName="Panel" Property="Background" Value="{Binding SelectedBackground}" />
But it had no effect. I suspected that I needed some kind of Path argument on the binding, but I had no idea what. I tried using XAMLSpy to maybe help me see what was going on (as far as hierarchy of elements and possible binding path), but I didn't get very far with that -- except that when I tried to set the SelectedBackground property through XAMLSpy, it reported that the property SelectedBackground wasn't found. How could that be? ....since I had compiled and run the program without error.
I hope what I'm trying to do makes sense -- I just want to change the background color of selected tabs on a tab control when they are selected.