I have a RepeatButton in a Toolbar. I want it to have the same visual appearance as the ToolBar-Buttons. But ToolBar.ButtonStyleKey doesn't work because of the different TargetType. So where are the built-in styles defined (for various Themes) and how can I clone the ToolBar.ButtonStyle for RepeatButton?
Asked
Active
Viewed 293 times
1
-
I can't help you with the 1st part of the question (Where are the default styles defined? In some theme(s), which again are stored in some DLL file, somewhere, i guess...). But to get the (default) control template for a control in XAML form, you might follow the advice given here: https://stackoverflow.com/a/26548268/2819245. – Nov 10 '18 at 15:31
-
Does this answer your question? [Lookup default styles in wpf?](https://stackoverflow.com/questions/19050771/lookup-default-styles-in-wpf) – StayOnTarget May 04 '21 at 15:37
1 Answers
3
They are defined in the C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework*
assemblies. You could use a decompiler such as dotPeek to extract the templates, or you can right-click on a control in design mode in Visual Studio or in Blend and choose Edit Template
->Edit a Copy
to copy the default template into your XAML markup.
I tried this and I got the
Template
forToolBar
, but not the style for ToolBarButton. The search ends at ToolBarButtonStyleKey, but can't find ToolBarButtonStyle for WPF.
It's right there:
<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="{x:Type Button}">
<Setter Property="Control.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Control.Padding" Value="2"/>
<Setter Property="Control.BorderThickness" Value="1"/>
<Setter Property="Control.Background" Value="Transparent"/>
<Setter Property="Control.BorderBrush" Value="Transparent"/>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
<Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
<Setter Property="Control.HorizontalContentAlignment" Value="Center"/>
<Setter Property="Control.VerticalContentAlignment" Value="Center"/>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Bd" Background="{TemplateBinding Control.Background}"
BorderBrush="{TemplateBinding Control.BorderBrush}"
BorderThickness="{TemplateBinding Control.BorderThickness}"
Padding="{TemplateBinding Control.Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="true">
<Setter TargetName="Bd" Value="{StaticResource ƻ}" Property="Border.BorderBrush"/>
<Setter TargetName="Bd" Value="{StaticResource ƺ}" Property="Border.Background"/>
</Trigger>
<Trigger Property="UIElement.IsKeyboardFocused" Value="true">
<Setter TargetName="Bd" Value="{StaticResource ƻ}" Property="Border.BorderBrush"/>
<Setter TargetName="Bd" Value="{StaticResource ƺ}" Property="Border.Background"/>
</Trigger>
<Trigger Property="ButtonBase.IsPressed" Value="true">
<Setter TargetName="Bd" Value="{StaticResource ƾ}" Property="Border.BorderBrush"/>
<Setter TargetName="Bd" Value="{StaticResource ƽ}" Property="Border.Background"/>
</Trigger>
<Trigger Property="UIElement.IsEnabled" Value="false">
<Setter Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Property="Control.Foreground"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>

mm8
- 163,881
- 10
- 57
- 88
-
Thanks mm8, I tried this and I got the Template for ToolBar, but not the style for ToolBarButton. The search ends at ToolBarButtonStyleKey, but can't find ToolBarButtonStyle for WPF. – ubsch Nov 14 '18 at 15:26
-