Message Only MessageBox

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageOnly_Click" Content="Message Only" Margin="5" Name="btnMessageOnly" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

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

        private void btnMessageOnly_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A simple MessageBox.");
        }

    }
}

   
    
     


MessageBox with Message and Header

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageHeader_Click" Content="Message and Header" Margin="5" Name="btnMessageHeader" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

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


        private void btnMessageHeader_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A MessageBox with a title.", "WPF");
        }

    }
}

   
    
     


Set Message, Header, and Button for MessageBox

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageHeaderButton_Click" Content="Message, Header, and Button" Margin="5" Name="btnMessageHeaderButton" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

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


        private void btnMessageHeaderButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A MessageBox with a title and buttons.",  "WPF", MessageBoxButton.YesNoCancel);
        }

    }
}

   
    
     


Customize Message, Header, Button, and Image for MessageBox

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageHeaderButtonImage_Click" Content="Message, Header, Button, and Image" Margin="5" Name="btnMessageHeaderButtonImage" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

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

        private void btnMessageHeaderButtonImage_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A MessageBox with a title, buttons, and an icon.", "WPF",
                MessageBoxButton.YesNoCancel,
                MessageBoxImage.Warning);
        }
    }
}

   
    
     


Use MenuItem.Tag to store user object

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="AppWindowsSample" Height="200" Width="300" 
    xmlns:my="clr-namespace:System;assembly=mscorlib">
  <StackPanel>
    <Menu Name="menu1" >
      <MenuItem Header="Window" x:Name="windowMenu">
        <MenuItem Header="SubmenuOpened event" />
      </MenuItem>
    </Menu>
    <Button Margin="10" Name="newWindowButton" Height="23" VerticalAlignment="Top">New Window</Button>
    <StatusBar Grid.Row="2" Name="statusBar1" >
      <TextBlock x:Name="statusText">Status</TextBlock>
    </StatusBar>
    <Button Height="23" Margin="10" Name="shutdownButton" VerticalAlignment="Top" >Shutdown</Button>
  </StackPanel>
</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 System.Windows.Controls.Primitives;
using System.Diagnostics;
using System.ComponentModel;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();

      DataContext = Application.Current.Windows;
      statusText.Text = Application.Current.ShutdownMode.ToString();

      newWindowButton.Click += new RoutedEventHandler(newWindowButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
      windowMenu.SubmenuOpened += new RoutedEventHandler(windowMenu_SubmenuOpened);
      Closing += Window1_Closing;
    }

    public void windowMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
      windowMenu.Items.Clear();
      foreach( Window window in Application.Current.Windows ) {
        MenuItem item = new MenuItem();
        item.Header = window.Title;
        item.Click += windowMenuItem_Click;
        item.Tag = window;
        item.IsChecked = window.IsActive;
        windowMenu.Items.Add(item);
      }
    }

    public void windowMenuItem_Click(object sender, RoutedEventArgs e) {
      Window window = (Window)((MenuItem)sender).Tag;
      window.Activate();
    }

    public void newWindowButton_Click(object sender, RoutedEventArgs e) {
      Window window = new Window();
      window.Title = "Window " + DateTime.Now;
      window.Width = 200;
      window.Height = 100;
      window.Show();
      statusText.Text = Application.Current.ShutdownMode.ToString();
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown();
    }

    void Window1_Closing(object sender, CancelEventArgs e) {
      if( MessageBox.Show("Do you really want to shut down?", "Shutting Down", MessageBoxButton.YesNo) == MessageBoxResult.No ) {
        e.Cancel = true;
      }
    }


  }
}

   
     


Format TextBox with MenuItem: normal, bold, italic

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="200" Width="300">
    <DockPanel LastChildFill="True">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_Edit">
                <MenuItem Header="Cu_t" Command="ApplicationCommands.Cut" />
                <MenuItem Header="_Copy" Command="ApplicationCommands.Copy" />
                <MenuItem Header="_Paste" Command="ApplicationCommands.Paste"/>
                <Separator/>
                <MenuItem Click="SelectAll_Click" Header="_Select All" />
                <MenuItem Click="Clear_Click" Header="_Clear" />
            </MenuItem>
            <MenuItem Header="_Format">
                <MenuItem Click="TextStyle_Click" Header="_Normal" Name="miNormal" />
                <MenuItem Click="TextStyle_Click" FontWeight="Bold" Header="_Bold" Name="miBold" />
                <MenuItem Click="TextStyle_Click" FontStyle="Italic" Header="_Italic" Name="miItalic" />
            </MenuItem>
        </Menu>
        <TextBox Name="txtTextBox" TextWrapping="Wrap">
            text
        </TextBox>
    </DockPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

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

   
     


MenuBar and ToolBar

image_pdfimage_print


   
 

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

    <ToolBarTray Grid.Row="1">
      <ToolBar>
        <Button Command="New">New</Button>
        <Button Command="Open">Open</Button>
        <Button Command="Save">Save</Button>
      </ToolBar>
      <ToolBar>
        <Button Command="Cut">Cut</Button>
        <Button Command="Copy">Copy</Button>
        <Button Command="Paste">Paste</Button>
      </ToolBar>
    </ToolBarTray>
    <TextBox Margin="5" Grid.Row="2" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>
      </TextBox.CommandBindings>
      
    </TextBox>
    <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()
        {
        }

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