Mega Code Archive

 
Categories / VB.Net Tutorial / WPF
 

Create Styles That Adapt to the Current OS Theme

<Window x: Class="WpfApplication1.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="WPF" Height="134" Width="200">     <Window.Resources>     <SolidColorBrush x:Key="ButtonText" Color="Black"/>     <Style         x:Key="CustomProgressBarStyle"         TargetType="{x:Type ProgressBar}">         <Setter Property="Template">             <Setter.Value>                 <ControlTemplate                          TargetType="{x:Type ProgressBar}">                     <Grid MinHeight="20" MinWidth="240">                         <Border                            Name="PART_Track"                            Background="{DynamicResource                              {x:Static SystemColors.InactiveCaptionBrushKey}}"                           BorderBrush="{DynamicResource                              {x:Static SystemColors.InactiveBorderBrushKey}}"                           BorderThickness="1"                                />                         <Border                            Name="PART_Indicator"                            Background="{DynamicResource                              {x:Static SystemColors.ActiveCaptionBrushKey}}"                           BorderBrush="{DynamicResource                              {x:Static SystemColors.ActiveBorderBrushKey}}"                           BorderThickness="1"                            HorizontalAlignment="Left"                               />                     </Grid>                 </ControlTemplate>             </Setter.Value>         </Setter>     </Style>     </Window.Resources>     <StackPanel>         <ProgressBar Value="30"                       HorizontalAlignment="Center"                      Margin="4"                      Style="{DynamicResource CustomProgressBarStyle}"/>         <Button Margin="4"                  Content="Custom Brush"                  Foreground="{DynamicResource ButtonText}"/>         <Button Margin="4"                  Content="System Brush"                  Foreground="{DynamicResource                     {x:Static SystemColors.ActiveCaptionBrushKey}}"/>     </StackPanel> </Window>