Display a Context Menu with Opacity


   
  
<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;
            }
        }
    }
}

   
    
     


Grid with ContextMenu


   
    
<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>

   
    
    
    
     


ContextMenu Demo


   
   

<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
      <HeaderedItemsControl Header="ContextMenu">
        <StackPanel>
          <Border Margin="8" Background="#EEE" Width="150" Height="50"  CornerRadius="2">
            <Border.ContextMenu>
              <ContextMenu>
                <MenuItem Header="Sub One" InputGestureText="Ctrl+L" />
                <MenuItem Header="Sub Two (With an Icon)" InputGestureText="Ctrl+A">
                  <MenuItem.Icon>
                    <Ellipse Width="16" Height="16" Fill="LightBlue" />
                  </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Sub Three" />
                <Separator />
                <MenuItem Header="Sub Four">
                  <MenuItem Header="Sub One" />
                  <MenuItem Header="Sub Two" />
                  <MenuItem Header="Sub Three" />
                </MenuItem>
                <MenuItem Header="Sub Five" />
              </ContextMenu>
            </Border.ContextMenu>
            <TextBlock Foreground="#AAA" VerticalAlignment="Center" HorizontalAlignment="Center">(Right-Click Me)</TextBlock>
          </Border>
        </StackPanel>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>

   
    
    
     


OpenFileDialog Test


   
  

<Window x:Class="Windows.OpenFileTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OpenFileTest" Height="300" Width="300" 
    >
  <DockPanel Margin="5">
    <Button DockPanel.Dock="Top" Click="cmdOpen_Click">Open</Button>
    <ListBox Name="lstFiles"></ListBox>
  </DockPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace Windows
{
    public partial class OpenFileTest : System.Windows.Window
    {
        public OpenFileTest()
        {
            InitializeComponent();
        }

        private void cmdOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog myDialog = new OpenFileDialog();

            myDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
            myDialog.CheckFileExists = true;
            myDialog.Multiselect = true;

            if (myDialog.ShowDialog() == true)
            {
                lstFiles.Items.Clear();
                foreach (string file in myDialog.FileNames)
                {
                    lstFiles.Items.Add(file);
                }                
            }
        }
    }
}

   
    
     


Handle a Button Click with Shared button click handler


   
  

<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="200" Width="200">
    <StackPanel>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="10" 
                Name="button1" Width="75">Button One</Button>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="10" 
                Name="button2" Width="75">Button Two</Button>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="10" 
                Name="button3" Width="75">Button Three</Button>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void SharedButtonClickHandler(object sender, RoutedEventArgs e)
        {
            Button source = e.OriginalSource as Button;

            if (source != null)
            {
                MessageBox.Show("You pressed " + source.Name, Title);
            }
        }
    }
}

   
    
     


Parse command line arguments and make them available to an application.

   
  


<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="CommandLineArgumentSample.App"
    StartupUri="MainWindow.xaml"
    Startup="app_Startup" />

//File:App.xaml.cs
namespace CommandLineArgumentSample
{
    using System;
    using System.Collections;
    using System.Text.RegularExpressions;
    using System.Windows;

    public partial class App : Application
    {
        void app_Startup(object sender, StartupEventArgs e)
        {
            if (e.Args.Length == 0) return;
            foreach (string arg in e.Args)
            {
                Console.WriteLine(arg);
            }
        }
    }
}

   
    
     


Use TextBox.CommandBindingst to bind command


   
  
<Window x:Class="Commands.TwoDocument"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TwoDocument" Height="300" Width="300">

  <Window.Resources>
    <CommandBinding x:Key="binding" Command="ApplicationCommands.Save" Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0">
      <MenuItem Header="File">
        <MenuItem Command="New"></MenuItem>
        <MenuItem Command="Open"></MenuItem>
        <MenuItem Command="Save"></MenuItem>
        <MenuItem Command="SaveAs"></MenuItem>
        <Separator></Separator>
        <MenuItem Command="Close"></MenuItem>
      </MenuItem>
    </Menu>

    <TextBox Margin="5" Grid.Row="3" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>
        <CommandBinding Command="ApplicationCommands.Save" Executed="SaveCommand" />
      </TextBox.CommandBindings>
    </TextBox>

  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Commands
{
    public partial class TwoDocument : System.Windows.Window
    {
        public TwoDocument()
        {
            InitializeComponent();
        }

        private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {            
        }
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
        }
        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }
    }
}