views:

72

answers:

3

Ok, I was programming an app that loaded merged dictionaries on runtime to change appearance and behaviour when I got stuck : some of the controls on my forms just weren't reacting to the styles I thought they had to react to.

I have tried to simplify the problem as much as I could and came up with something so simple that I'm afraid I am overlooking the bleeding obvious, but anyway here it goes :

<Window x:Class="Example.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="100" Width="50">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="Background" Value="Green"></Setter>
                        </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>        
        <TextBox Text="1" Name="box1"/>
        <TextBox Text="2" Name="box2"/>
        <TextBox Text="3" Name="box3"/>
    </StackPanel>
</Window>

The question is : why is the first textbox not green? ==> that is, the designer shows it in green, but when running the app, it is no longer...

I know the solotion to this particular problem is to remove the merged dicitonary tags, but I need to know how to solve this using merged dictionaries.

Thanks!

A: 

Try this...

<Style TargetType="{x:Type TextBox}">
Akash Kava
Nope, that's not it, tx anyway, I have changed the example now
Peter
A: 

I'm not sure if this will work, but when I was having styling problems, I fixed them by using

TargetType="{x:Type TextBox}"
Pavel
Nope, that's not it, tx anyway, I have changed the example now
Peter
+1  A: 

MergedDictionaries have always been quirky, you can set any resources you want in them, but they only process outside resource dictionary references at runtime.

    <ResourceDictionary.MergedDictionaries>

        <ResourceDictionary Source="StylesDictionary.xaml"/>

    </ResourceDictionary.MergedDictionaries>

The only requirement is that the resources be set in an other dictionary.

kek444
Well, I concider that a workaround, cause in the documentation it states that mixing them with insides is ok, and ofcourse since 2 out or 3 textboxes are treated correctly! BUT, it is certainly a solution for my problem! tx!
Peter