Grid with ContextMenu

image_pdfimage_print


   
    
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid Background="Transparent">
  <Grid.ContextMenu>
    <ContextMenu>
      <MenuItem Header="Foo" />
      <MenuItem Header="Bar" />
    </ContextMenu>
  </Grid.ContextMenu>


</Grid>
</Page>

   
    
    
    
     


Display a Context Menu with Opacity

image_pdfimage_print


   
  
<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="100" Width="300">
    <Grid>
        <TextBox FontSize="16"  Height="23" Name="txtTextBox" >
            <TextBox.ContextMenu>
                <ContextMenu HasDropShadow="True" Opacity=".8">
                    <MenuItem Command="Cut" Header="Cu_t" />
                    <MenuItem Command="Copy" Header="_Copy" />
                    <MenuItem Command="Paste" Header="_Paste" />
                    <Separator/>
                    <MenuItem Click="SelectAll_Click" Header="_Select All" />
                    <MenuItem Click="Clear_Click" Header="_Clear" />
                    <Separator/>
                    <MenuItem Header="Format">
                        <MenuItem Click="TextStyle_Click" Header="_Normal" Name="miNormal"></MenuItem>
                        <MenuItem Click="TextStyle_Click" FontWeight="Bold" Header="_Bold" Name="miBold"></MenuItem>
                        <MenuItem Click="TextStyle_Click" FontStyle="Italic" Header="_Italic" Name="miItalic"></MenuItem>
                    </MenuItem>
                </ContextMenu>
            </TextBox.ContextMenu>
            A TextBox control with ContextMenu.
        </TextBox>
    </Grid>
</Window>
//File:Window.xaml.cs
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            txtTextBox.Clear();
        }
        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            txtTextBox.SelectAll();
        }
        private void TextStyle_Click(object sender, RoutedEventArgs e)
        {
            if (sender == miNormal)
            {
                txtTextBox.FontWeight = FontWeights.Normal;
                txtTextBox.FontStyle = FontStyles.Normal;
            }
            else if (sender == miBold)
            {
                txtTextBox.FontWeight = FontWeights.Bold;
            }
            else if (sender == miItalic)
            {
                txtTextBox.FontStyle = FontStyles.Italic;
            }
        }
    }
}

   
    
     


Control template: event trigger, border and text

image_pdfimage_print


   
      
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <ControlTemplate x:Key="btnCustom" TargetType="{x:Type Button}">
            <Border Name="border" BorderThickness="3" BorderBrush="Blue"
                    Background="{TemplateBinding Foreground}">

                <TextBlock Name="txtblk" 
                           FontStyle="Italic" 
                           Text="{TemplateBinding Content}"
                           Margin="{TemplateBinding Padding}"
                           Foreground="{TemplateBinding Background}" />
            </Border>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="border"
                            Property="CornerRadius" Value="12" />
                    <Setter TargetName="txtblk"
                            Property="FontWeight" Value="Bold" />
                </Trigger>

                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="border"
                            Property="Background" 
                            Value="{Binding Path=Background}" />
                    <Setter TargetName="txtblk"
                            Property="Foreground"
                            Value="{Binding Path=Foreground}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Page.Resources>

    <StackPanel>
        <Button Template="{StaticResource btnCustom}"
                HorizontalAlignment="Center" Margin="24"
                FontSize="24"   >
            Button
        </Button>
    </StackPanel>
</Page>

   
    
    
    
    
    
     


Put a Control Template into a Style

image_pdfimage_print


   
      
<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="120" Width="260">

    <Window.Resources>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Width" Value="36"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="Template" >
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ToggleButton}">
                        <Canvas >
                            <Path x:Name="pth" Stroke="#000080" Fill="#C0C0C0" 
                              StrokeThickness="3" StrokeStartLineCap="Round" 
                              StrokeEndLineCap="Round" StrokeLineJoin="Round" 
                              Data="M 10,100 l 100,0 l 18,-10 l 5,110 l 10,0 l -7,110 l 2,210 l -20,-15 l -110,5 l 2,-110 Z" />
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" Orientation="Horizontal">
        <ToggleButton IsChecked="True"/>
        <ToggleButton IsChecked="False"/>
    </StackPanel>

</Window>

   
    
    
    
    
    
     


Label with ControlTemplate

image_pdfimage_print


   
      


<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="100" Width="180">

    <Window.Resources>
        <ControlTemplate x:Key="labelTemplate" TargetType="{x:Type Label}">
            <Border x:Name="border" CornerRadius="4" BorderThickness="3" BorderBrush="DarkGray" Background="{TemplateBinding Property=Background}">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Label Width="100" Height="24" 
               Margin="4" Content="Custom Label"
               Template="{StaticResource labelTemplate}"
               Background="Red"/>
    </Grid>
</Window>

   
    
    
    
    
    
     


Specify Named Parts of a Control Template

image_pdfimage_print


   
      

<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="120" Width="300">
    <Window.Resources>
        <Style
            TargetType="{x:Type ProgressBar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ProgressBar}">
                        <Grid MinHeight="20" MinWidth="240">
                            <Rectangle Name="PART_Track" Fill="Gainsboro" Stroke="Gray" StrokeThickness="1" />
                            <Rectangle Name="PART_Indicator" Fill="DarkGray" Stroke="Gray" StrokeThickness="1" HorizontalAlignment="Left" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <ProgressBar  Width="200" Height="100" x:Name="progress" Value="30" HorizontalAlignment="Center" Margin="10"/>
        <Slider Value="{Binding ElementName=progress, Path=Value, Mode=TwoWay}" Minimum="0" Maximum="100"  Margin="10"/>
    </StackPanel>
</Window>

   
    
    
    
    
    
     


ProgressBar with ControlTemplate

image_pdfimage_print


   
      

<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="120" Width="300">
    <Window.Resources>
        <Style
            TargetType="{x:Type ProgressBar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ProgressBar}">
                        <Grid MinHeight="20" MinWidth="240">
                            <Rectangle Name="PART_Track" Fill="Gainsboro" Stroke="Gray" StrokeThickness="1" />
                            <Rectangle Name="PART_Indicator" Fill="DarkGray" Stroke="Gray" StrokeThickness="1" HorizontalAlignment="Left" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <ProgressBar Width="200" Height="100"  x:Name="progress" Value="30" HorizontalAlignment="Center" Margin="10"/>
        <Slider Value="{Binding ElementName=progress, Path=Value, Mode=TwoWay}" Minimum="0" Maximum="100"  Margin="10"/>
    </StackPanel>

</Window>