开发者

Overriding themes in WPF

开发者 https://www.devze.com 2023-01-13 15:31 出处:网络
I have a got a small issue with the wpf project I am currently working on. I am new to WPF. In my app.xaml I am using the Microsoft Aero theme for my application. I have got something like this in my

I have a got a small issue with the wpf project I am currently working on. I am new to WPF. In my app.xaml I am using the Microsoft Aero theme for my application. I have got something like this in my app.xaml

 <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.NormalColor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

Now what I would like to know how to override certain style properties e.g. For Buttons I want to override the font style meanwhile retaining the rest of the aero style.

If I define a style for button in my window resources e.g.

 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
            <Setter Property="Foreground" Value="Red" />
        </Style>

and define a button based on the above style

the button looses all the aero style properties. I think the problem is to do with the way I define the BasedOn property for the style

BasedOn="{StaticResource {x:Type Button}}"

I don't think the resource is sta开发者_如何学运维tic as it comes from a dll, it should probably be something like this. Not sure though.

BasedOn="{DynamicResource {x:Type Button}}"

but the above throws exception and what if I had multiple resourcedictionary in my app.xaml e.g. luna and classic. How can I tell which one to use as the default and at the sametime using and overriding another (e.g. luna) specifically for certain controls in my ui? So some of my buttons will be based on luna style and some on aero style with some further modifications?

Any idea?

Regards,


For your specific question on

e.g. For Buttons I want to override the font style meanwhile retaining the rest of the aero style.

I'd try adding this in your Resource Dictionary:

<FontFamily x:Key="YourFont">Tahoma</FontFamily>

Then use this in your Style:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
     <Setter Property="FontFamily" Value="{StaticResource YourFont}"/>
</Style>

So you could change the FontFamily based on one single key.

I recommend looking at this http://wpfthemes.codeplex.com/SourceControl/latest#WPF.Themes.Demo/Window1.xaml

0

精彩评论

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