开发者

WPF Replacing AND extending styles

开发者 https://www.devze.com 2023-03-23 08:18 出处:网络
How do I change what WPF\'s idea of the开发者_高级运维 default style for a control is?And why is this happening in the first place?In the below XAML, I declare a Style for Button, and then further dec

How do I change what WPF's idea of the开发者_高级运维 default style for a control is? And why is this happening in the first place? In the below XAML, I declare a Style for Button, and then further declare a new Style that overrides one of the setters called "HugeBut". I would expect that HugeBut is implicitly BasedOn my new un-named style, but apparently it is not;

<Window.Resources>
  <Style TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
          <Border Background="Red">
            <ContentPresenter/>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!-- badness -->
  <Style TargetType="{x:Type Button}" x:Key="HugeBut">
    <Setter Property="Foreground" Value="Yellow"/>
  </Style>

  <!-- works, but I do not want to explicitly set the based-on. -->
  <Style TargetType="{x:Type Button}" x:Key="HugeBut" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Foreground" Value="Yellow"/>
  </Style>
</Window.Resources>

<Button Content="Regular" />
<Button Content="huge!" Style="{StaticResource HugeBut}"/>

You would expect two red buttons, one with black text and one with yellow, but Style HugeBut inherits all of the values that I did not specify in my unnamed style from the system default theme for Button (Aero in my case).

What can I do to change this behavior?


It appears that the answer is here:

http://wpfthemereplacer.codeplex.com/

From the site description:

This library allows users to provide their own resource dictionaries to replace the default theme dictionaries loaded by WPF. This makes it so you don't have to decorate custom styles with BasedOn="{StaticResource {x:Type ...}}" when your own custom theme is being used in your application. It also makes it so if you have custom controls that just provide enhanced capability and don't need to replace the the style, you don't need to define a new style or override the DefaultStyleKey when you create the custom control.

This is exactly what I'm looking for. This will allow me to use Styles as they are meant to be used across an app that has been extensively "re-themed", rather than theme-ing by setting global styles (and then deal with tracking down bits of code that are missing BasedOn, or cannot deal with it at all due to WPF bugs and other constraints)


works, but I do not want to explicitly set the based-on.

Well, the framework does not really care if you don't want to, for all i know, you have to.

0

精彩评论

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