msedi
msedi

Reputation: 1733

Override specific properties of a style

I'm creating a new style for the TabControl including a new ItemContainerStyle for the items. The new style works fine, except that I need the possibility to add features to the ItemContainerStyle when using the style. In detail this is the Header property

  <TabControl x:Name="myTabControl" SelectionChanged="myTabControl_SelectionChanged">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding Title}"></Setter>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>

which works OK. But it overrides the style completely. the BasedOn property would help, but I don't have access to the key of the ItemContainerStyle since it is embedded in the TabControls' style. How can I simply update a property of the style without overriding the style completely?

Thanks

Upvotes: 2

Views: 187

Answers (2)

Fratyx
Fratyx

Reputation: 5797

You can base your style on the implicitly applied default style:

<Style TargetType="TabItem" BasedOn="{StaticResource {x:Type TabItem}}">

Upvotes: 0

Matt Wilkinson
Matt Wilkinson

Reputation: 590

There is a bit of a difference in the way styles in WPF compared to css. In Wpf they work completely off of inheritance, this is the basic document on how styles work. So if a key is not provided for you I think you are out of luck when using the BasedOn inheritance.

However, Microsoft does provide a useful utility in Visual Studio Blend. In the Objects and Timelines Window you right click then select an "edit a style", This will do all the heavy lifting for you. If you are going to be doing a lot of small changes on the style I would suggest you make a copy, and give it a Key, then use the BasedOn property to make your small changes that you want.

I Hope this helps.

Upvotes: 1

Related Questions