Skip to content

Instantly share code, notes, and snippets.

@battleguard
Created January 23, 2015 05:16
Show Gist options
  • Select an option

  • Save battleguard/cdf6b48d6a7bfd7c01e5 to your computer and use it in GitHub Desktop.

Select an option

Save battleguard/cdf6b48d6a7bfd7c01e5 to your computer and use it in GitHub Desktop.
Proper Style Inheritance in WPF
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WpfApplication1.Controls"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type Control}" x:Key="MetroControl">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource MetroControl}"/>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MetroControl}"/>
<Style TargetType="{x:Type controls:CircleButton}" BasedOn="{StaticResource MetroControl}"/>
<Style TargetType="{x:Type Window}" BasedOn="{StaticResource MetroControl}" x:Key="MetroWindow">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontFamily" Value="arial"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
<Style TargetType="{x:Type Window}" BasedOn="{StaticResource MetroWindow}"/>
</Application.Resources>
</Application>
<Button x:Class="WpfApplication1.Controls.CircleButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Button.Template>
<ControlTemplate>
<Grid Width="50" Height="50" Background="Green">
<TextBlock Text="T" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WpfApplication1.Controls"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:attachedProperties="clr-namespace:WpfApplication1.AttachedProperties"
Title="MainWindow" Height="350" Width="525" Style="{StaticResource MetroWindow}">
<StackPanel>
<Label Content="Test Label"/>
<Button Content="Test Button"/>
<TextBlock Text="Test TextBlock" Background="BlueViolet"/>
<controls:CircleButton Click="ButtonBase_OnClick">
<i:Interaction.Behaviors>
<attachedProperties:PopupButtonTestBehavior PopupPlacement="MousePoint" PopupHorizontalOffset="20">
<attachedProperties:PopupButtonTestBehavior.Popup>
<StackPanel Width="100" Height="100" Background="Black">
<TextBlock Text="Textblock"/>
<Label Content="Label"/>
<Button Content="Button"/>
</StackPanel>
</attachedProperties:PopupButtonTestBehavior.Popup>
</attachedProperties:PopupButtonTestBehavior>
</i:Interaction.Behaviors>
</controls:CircleButton>
</StackPanel>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment