Desktop to Control

image_pdfimage_print


   
   

<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.SampleViewer"
    Title="Examples" >

   <Canvas> 
        <Button Width="120" Height="20">
          <Button.Background>
            <LinearGradientBrush>
              <LinearGradientBrush.GradientStops>
                  <GradientStop Offset="0" Color="{x:Static SystemColors.DesktopColor}"/>
                  <GradientStop Offset="1" Color="{x:Static SystemColors.ControlColor}"/>
              </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
          </Button.Background>
        </Button> 

   </Canvas> 


</Window>

   
    
    
     


Bind Label To ScrollBar

image_pdfimage_print


   
   
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ScrollBar Name="scroll"
               Orientation="Horizontal" Margin="24" 
               Maximum="100" LargeChange="10" SmallChange="1" />

    <Label HorizontalAlignment="Center" 
           Content="{Binding ElementName=scroll, Path=Value}" />

</StackPanel>

   
    
    
     


Bind ScrollBar To Label

image_pdfimage_print


   
   

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ScrollBar Orientation="Horizontal" Margin="24" 
               Maximum="100" LargeChange="10" SmallChange="1"
               Value="{Binding ElementName=lbl, Path=Content}" />

    <Label Name="lbl" Content="50"
           HorizontalAlignment="Center" />

</StackPanel>

   
    
    
     


Binding With Data Context

image_pdfimage_print


   
   
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ScrollBar Name="scroll"
               Orientation="Horizontal" Margin="24" 
               Maximum="100" LargeChange="10" SmallChange="1" />

    <Label HorizontalAlignment="Center"
           DataContext="{Binding ElementName=scroll}"
           Content="{Binding Path=Value}" />

</StackPanel>

   
    
    
     


Shut down the application in Window closing event

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:system="clr-namespace:System.Windows;assembly=PresentationFramework"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="310" Width="280" Closing="Window1_Closing">

</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Window1_Closing(object sender, CancelEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}

   
    
     


Menu with Application command: cut, copy, paste

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

   
    
     


Use Application Command to edit RichTextBox

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="350" Width="500">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="CommandTarget" Value="{Binding ElementName=rtbTextBox1}" />
                </Style>
            </StackPanel.Resources>
            <Button Content="Clear" Name="btnClear" Click="btnClear_Click" />
            <Separator Margin="5"/>
            <Button Content="Cu_t" Command="ApplicationCommands.Cut" />
            <Button Content="_Copy" Command="ApplicationCommands.Copy" />
            <Button Content="_Paste" Command="ApplicationCommands.Paste" />
            <Separator Margin="5"/>
            <Button Content="_Undo" Command="ApplicationCommands.Undo" />
            <Button Content="_Redo" Command="ApplicationCommands.Redo" />
            <Separator Margin="5"/>
            <Button Content="_Bold" Command="EditingCommands.ToggleBold" />
            <Button Content="_Italic" Command="EditingCommands.ToggleItalic" />
            <Button Content="Underline" Command="EditingCommands.ToggleUnderline" />
            <Separator Margin="5"/>
            <Button Content="_Right" Command="EditingCommands.AlignRight" />
            <Button Content="C_enter" Command="EditingCommands.AlignCenter" />
            <Button Content="_Left" Command="EditingCommands.AlignLeft" />
        </StackPanel>
        <RichTextBox DockPanel.Dock="Bottom" Name="rtbTextBox1"
                     HorizontalScrollBarVisibility="Visible" 
                     VerticalScrollBarVisibility="Visible">
            <FlowDocument>
                <Paragraph FontSize="12">
                    paragraph
                </Paragraph>
                <Paragraph FontSize="15">
                    paragraph
                </Paragraph>
                <Paragraph FontSize="18">A List</Paragraph>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
            </FlowDocument>
        </RichTextBox>
    </DockPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            rtbTextBox1.SelectAll();
            rtbTextBox1.Cut();
        }
    }
}