views:

138

answers:

1

In Charles Petzold's "Using Templates to Customize WPF Controls" article in the Jan 2007 edition of MSDN Magazine (http://msdn.microsoft.com/en-us/magazine/cc163497.aspx), he says,

The ProgressBar control actually has two default templates for the two orientations. (This is true of ScrollBar and Slider, as well.) If you want your new ProgressBar to support both orientations, you should write two separate templates and select them in the Triggers section of a Style element that you also define for the ProgressBar.

I am currently writing a custom control that requires this functionality, but can't work out how to do as he says - not in any way that works, anyhow. Does anybody have a sample of this?

Thanks in advance.

+2  A: 

You can see how its done in the scrollbar sample control template http://msdn.microsoft.com/en-us/library/ms742173.aspx

I copied only the relevant parts of the sample here.

In short there are two templates in the resource dictionary:

<ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}"> ...

<ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}"> ...

And a trigger in the style to switch between them :

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
  <Style.Triggers>
    <Trigger Property="Orientation" Value="Horizontal">
      <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
    </Trigger>
    <Trigger Property="Orientation" Value="Vertical">
      <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
    </Trigger>
  </Style.Triggers>
</Style>
Nir