Resources defined in UserControl.Resources can not see elements defined in MergedDictionaries set from code
DefaultStyle
s contains a DefaultStyle
for all TextBox
es:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
Resources.MergedDictionaries.Add(new DefaultStyles());
InitializeComponent();
}
}
Then the Xaml, I inherit the style and add a little more:
<UserControl.Resources>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Green "/>
</Style>
</UserControl.Resources>
This throws a StackOverFlowException
as my DefaultStyle
is not found开发者_StackOverflow中文版, and so it trys to self reference.
Why can't the Style see the default styles in the merged dictionary?
It can't see it because it's essentially replacing it. The InitializeComponent
method call will replace the dictionary you refer to in your ctor with the dictionary you define in your XAML.
Basically, you're doing it wrong. Why can't you just do this:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DefaultStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
...
</Style>
</ResourceDictionary>
</UserControl.Resources>
精彩评论