开发者

Resources defined in UserControl.Resources can not see elements defined in MergedDictionaries set from code

DefaultStyles contains a DefaultStyle for all TextBoxes:

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜