A style in WPF can be inherited. Usually, in real world scenarios most of the theme related styles will be merged in App.xaml. In such cases, if style needs to be defined again in scope of a view. That particular style will override the style defined in App. For example, the following style for ListBoxItem is defined in App.xaml.


<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Orange" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Padding" Value="10" />
</Style>

The above style clearly stick to theming. Since it is an implicit styling, the styling will apply to ListBox items defined below,


<ListBox ItemsSource="{Binding Movies}"
DisplayMemberPath="Name" />

demo1The Listbox displaying the movies and showing the name of movie in each item. Now if we want to use ItemContainerStyle property of ListBoxItem to bind some other data, the framework will prefer shortest path to pick style and style that defined in App.xaml will be ignored.


<ListBox ItemsSource="{Binding Movies}"
DisplayMemberPath="Name">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Opacity"
Value="{Binding Rating}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

demo2

Here I need both the styles I defined in App.xaml and the inline one. WPF provides a way to inherit the style by another one. BasedOn property helps us to achieve this.


<ListBox ItemsSource="{Binding Movies}"
DisplayMemberPath="Name">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem"
BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="Opacity"
Value="{Binding Rating}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

demo3Since the style in App.xaml is implicit style without key, we are specifying the type for BasedOn attribute. Otherwise we have to specify the key of base style.