I'm in pretty new to C# WPF and recently meet a very weird problem.
I'm managing style sheet xaml files as following pictures and code.
[ App.xaml ]
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/StyleText.xaml"/>
<ResourceDictionary Source="Styles/StyleButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
[ StyleText.xaml ]
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
[ StyleButton.xaml]
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock x:Name="TblockTest" Background="Gray" Foreground="Blue">
<TextBlock.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Green"/>
</Style>
</TextBlock.Resources>
<ContentPresenter x:Name="CpCustomButton" ContentSource="Content"/>
</TextBlock>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="CpCustomButton" Property="TextBlock.Foreground" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
These are test codes to reproduce the problem.
What I want to do is ignore the style of StyleText.xaml inside StyleButton.xaml without addressing key to the textblock style in StyleText.xaml
As you know, textblock style without key will serve the global style, and so I don't have to insert annoying 'style={~~}' code per textblock.
But now, I can't avoid that it infects the style of textblock inside of control template for other control like button.
As you can see in the StyleButton.xaml, I tried several things that I can think and googled so much times, but nothing solves this problem clearly.
The content of a button always shows in Red which is the color addressed by StyleText.xaml.
What should I do to solve the problem?
I also attach my test solution noted above.
https://www.dropbox.com/s/no0j60px2qnl51y/WeirdProblem.zip
Thanks to read.