Click here to Skip to main content
15,889,034 members
Articles / Desktop Programming / WPF

Visual Studio 2012 Metro Styles for WPF

Rate me:
Please Sign up or sign in to vote.
4.77/5 (75 votes)
9 Dec 2012CPOL8 min read 363.2K   60.8K   222  
Black Metro Styles for Button, ListBox, Menu, ScrollBar, TabControl, TextBox, ComboBox, DataGrid and GroupBox
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.Core;component/Resources.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="20"/>
            </Grid.ColumnDefinitions>
            <Border SnapsToDevicePixels="True" x:Name="Border" Grid.ColumnSpan="2" Background="{StaticResource BackgroundNormal}" BorderBrush="{StaticResource BorderBrushNormal}" BorderThickness="1" />
            <Path x:Name="Arrow" Grid.Column="1" Opacity="0.6" Fill="{StaticResource Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                <Setter TargetName="Arrow" Property="Opacity" Value="1" />
                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BorderBrushHighlighted}"/>
            </Trigger>
            <Trigger Property="ToggleButton.IsChecked" Value="true">
                <Setter TargetName="Arrow" Property="Opacity" Value="1" />
                <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource BackgroundSelected}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
        <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
    </ControlTemplate>

    <Style x:Key="StandardComboBox" TargetType="ComboBox">
        <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="MinWidth" Value="120"/>
        <Setter Property="MinHeight" Value="20"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton Name="ToggleButton" 
                                      Template="{StaticResource ComboBoxToggleButton}" 
                                      Grid.Column="2" 
                                      Focusable="false" 
                                      IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
                                      ClickMode="Press"/>
                        <ContentPresenter Name="ContentSite" 
                                          IsHitTestVisible="False"  
                                          Content="{TemplateBinding SelectionBoxItem}" 
                                          ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
                                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
                                          Margin="3,3,23,3" 
                                          VerticalAlignment="Center" 
                                          HorizontalAlignment="Left"/>
                        <TextBox x:Name="PART_EditableTextBox" 
                                 CaretBrush="{StaticResource Foreground}"
                                 Style="{x:Null}" 
                                 Template="{StaticResource ComboBoxTextBox}" 
                                 HorizontalAlignment="Left" 
                                 VerticalAlignment="Center" 
                                 Margin="3,3,23,3" 
                                 Focusable="True" 
                                 Background="Transparent" 
                                 Foreground="{StaticResource Foreground}" 
                                 Visibility="Hidden" 
                                 IsReadOnly="{TemplateBinding IsReadOnly}"/>
                        <Popup VerticalOffset="-1" SnapsToDevicePixels="True" Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True"  Focusable="False" PopupAnimation="Slide">
                            <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border x:Name="DropDownBorder" Background="{StaticResource BackgroundNormal}" BorderThickness="1" BorderBrush="{StaticResource BorderBrushNormal}"/>
                                <ScrollViewer SnapsToDevicePixels="True">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="false">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger Property="IsEditable" Value="true">
                            <Setter Property="IsTabStop" Value="false"/>
                            <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Resources>
            <Style TargetType="ComboBoxItem">
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ComboBoxItem">
                            <Border Name="Border" Padding="2" SnapsToDevicePixels="true" BorderThickness="1">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsHighlighted" Value="true">
                                    <Setter TargetName="Border" Property="Background" Value="{StaticResource BackgroundHighlighted}"/>
                                    <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BorderBrushHighlighted}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
    </Style>
</ResourceDictionary>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
I´m a student and hobby programmer from Germany.

Comments and Discussions