开发者

Defining other resources clobbers theme in WPF

I am building some WPF pages in a win forms application. I wish to use WPF Themes for my application. Not having an App.xaml (because the project i开发者_Python百科s a win forms project with a WPF ElementHost to show the WPF forms) I added my theme resource dictionary in my form like this:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Themes/ExpressionDark.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

This works for all controls on this form plus some of the other WPF user controls, but there are some user controls where the theme is not functioning. I have tracked down that the affected controls that define their own resources like this:

    <Grid.Resources>
        <Style TargetType="ComboBox">
            <Setter Property="Margin" Value="0 2 5 2" />
        </Style>
    </Grid.Resources>

It doesn't seem to matter where the resources are located or what is in the resource. Anything in the resource that is not keyed blocks the theme for everything that style targets. I even tried to out-smart it by finding the resource in the dictionary:

<Style TargetType="{x:Type ComboBox}">
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" />
</Style>

And modifying my style to look like this:

    <Grid.Resources>
        <Style TargetType="ComboBox">
            <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" />
            <Setter Property="Margin" Value="0 2 5 2" />
        </Style>
    </Grid.Resources>

But that caused my combo boxes to disappear completely!

Is there any way to add resources that blanket target controls and not throw off the theme?


You just need to set the BasedOn property of the Style to the old implicit Style:

<Grid.Resources>
    <Style x:Key="ComboBoxStyle" 
           TargetType="{x:Type ComboBox}" 
           BasedOn="{StaticResource {x:Type ComboBox}}">
        <Setter Property="Margin" Value="0 2 5 2" />
    </Style>

</Grid.Resources>

edit

So, if you don't want to use the resource key on all of your ComboBox's, you have to take it one step further. I don't know why WPF / WinForms interop handles implicit styles so poorly, but even if you add an implicit style to the resources here, and set BasedOn="{StaticResource ComboBoxStyle}", you will still lose your template.

However, if you move the resources to the UserControl, you can then do the implicit style properly:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Themes/ExpressionDark.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="ComboBoxStyle" 
               TargetType="{x:Type ComboBox}"
               BasedOn="{StaticResource {x:Type ComboBox}}">
            <Setter Property="Margin" Value="0 2 5 2" />
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type ComboBox}" 
                   BasedOn="{StaticResource ComboBoxStyle}" />
    </Grid.Resources>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜