Button PreviewMouseDown action and MouseDown action

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="AddHandler" Height="300" Width="300">
  <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button PreviewMouseDown="PreviewMouseDownButton" MouseDown="MouseDownButton">

      <Grid >
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Canvas Width="20" Height="18" VerticalAlignment="Center">

          <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16"
                   Fill="Yellow" Stroke="Black" />
        </Canvas>

      </Grid>
    </Button>
  </Grid>
</Window>
//File:Window.xaml.cs


using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

        }
       
        void PreviewMouseDownButton(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("PreviewMouseDownButton"); 
        }
 
        void MouseDownButton(object sender, RoutedEventArgs e){ 
            Debug.WriteLine("MouseDownButton"); 
        }


    }
}

   
    
     


Button click action

image_pdfimage_print


   
  

<Window x:Class="EightBall.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Eight Ball Answer" Height="328" Width="412" >
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.Background>
      <LinearGradientBrush>
        <LinearGradientBrush.GradientStops>
          <GradientStop Offset="0.00"  Color="Red" />
          <GradientStop Offset="0.50" Color="Indigo" />
          <GradientStop Offset="1.00" Color="Violet" />
        </LinearGradientBrush.GradientStops>
      </LinearGradientBrush>
    </Grid.Background>
    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtQuestion" 
             TextWrapping="Wrap" FontFamily="Verdana" FontSize="24"
             Grid.Row="0" >
      [Place question here.]
    </TextBox>
    <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer"
            Click="cmdAnswer_Click" 
            Grid.Row="1">      
      Ask the Eight Ball
      </Button>
    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtAnswer" 
             TextWrapping="Wrap" IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green"
             Grid.Row="2">
      [Answer will appear here.]
    </TextBox>    
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Input;

namespace EightBall
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void cmdAnswer_Click(object sender, RoutedEventArgs e)
        {           
            this.Cursor = Cursors.Wait;
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

            txtAnswer.Text = "asdf";
            this.Cursor = null;
        }

    }
}

   
    
     


Dynamically add Button to a Grid and add Action listener

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="DynamicXAML" Height="300" Width="300">
  <Grid Name="grid1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Markup;
using System.Xml;
using System.IO;

namespace WpfApplication1
{

  public partial class Window1 : System.Windows.Window
  {

    public Window1()
    {
      InitializeComponent();

      StringReader sr = new StringReader(@"<Button xmlns=&#039;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#039; 
        Foreground=&#039;BurlyWood&#039; FontSize=&#039;20pt&#039;>Click Me!</Button>");

      XmlReader reader = XmlReader.Create(sr);

      Button dynamicButton = (Button)XamlReader.Load(reader);

      this.grid1.Children.Add(dynamicButton);

      dynamicButton.Click += button1_Click;
   
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Dynamic Button Loaded From XAML String");
    }

  }
}

   
    
     


Do event based on button name

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="150" Width="250">
    <StackPanel>

        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Margin" Value="2" />                        
                    <EventSetter Event="Click" Handler="btnShowPopup_Click" />
                </Style>
            </StackPanel.Resources>
            <Button Content="Show Popup" Name="btnShowPopup" />
            <Button Content="Fade Popup" Name="btnFadePopup" />
            <Button Content="Scroll Popup" Name="btnScrollPopup" />
            <Button Content="Slide Popup" Name="btnSlidePopup" />
        </StackPanel>
    </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;
using System.Windows.Controls.Primitives;

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

        private void btnShowPopup_Click(object sender, RoutedEventArgs e)
        {
            if (sender == btnFadePopup)
            {
                Console.WriteLine("fade");
            }
            else if (sender == btnScrollPopup)
            {
                Console.WriteLine("scroll");
            }
            else if (sender == btnSlidePopup)
            {
                Console.WriteLine("slide");
            }
            else
            {
                Console.WriteLine("else");

            }
        }
    }
}

   
    
     


Formatted Digital Clock

image_pdfimage_print


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:src="clr-namespace:MyNameSpace.FormattedDigitalClock"
        Title="Formatted Digital Clock">
    <Window.Resources>
        <src:ClockTicker x:Key="clock" />
        <src:FormattedTextConverter x:Key="conv" />
    </Window.Resources>
    <Window.Content>
        <Binding Source="{StaticResource clock}" Path="DateTime" 
                 Converter="{StaticResource conv}" 
                 ConverterParameter="... {0:T} ..." />
    </Window.Content>
</Window>
//File:Window.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
using System.Globalization;
using System.Windows.Data;

namespace MyNameSpace.FormattedDigitalClock
{
    public class ClockTicker : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public DateTime DateTime
        {
            get { return DateTime.Now; }
        }
        public ClockTicker()
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += TimerOnTick;
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Start();
        }

        void TimerOnTick(object sender, EventArgs args)
        {
            if (PropertyChanged != null)
                PropertyChanged(this,new PropertyChangedEventArgs("DateTime"));
        }
    }
    public class FormattedTextConverter : IValueConverter{
        public object Convert(object value, Type typeTarget,object param, CultureInfo culture)
        {
            if (param is string)
                return String.Format(param as string, value);

            return value.ToString();
        }
        public object ConvertBack(object value, Type typeTarget,object param, CultureInfo culture)
        {
            return null;
        }
    }
}

   
    
     


Manual Update Target

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"
  xmlns:local="clr-namespace:WpfApplication1" 
  Title="ManualUpdateTarget" Height="135" Width="200">
  <Window.Resources>
    <local:Person x:Key="Tom" Name="Tom" Age="11" />
  </Window.Resources>
  <StackPanel DataContext="{StaticResource Tom}">
    <TextBlock Margin="5" VerticalAlignment="Center">Name:</TextBlock>
    <TextBox Margin="5" Name="nameTextBox" Text="{Binding Path=Name}" />
    <TextBlock Margin="5" VerticalAlignment="Center">Age:</TextBlock>
    <TextBox Margin="5" Name="ageTextBox" Text="{Binding Path=Age}" />
    <Button Margin="5" Width="75" Name="birthdayButton">Birthday</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.ComponentModel;

namespace WpfApplication1 {
  public class Person {
    string name;
    public string Name {
      get { return this.name; }
      set {
        if( this.name == value ) { return; }
        this.name = value;
      }
    }

    int age;
    public int Age {
      get { return this.age; }
      set {
        if( this.age == value ) { return; }
        this.age = value;
      }
    }

    public Person() { }
    public Person(string name, int age) {
      this.name = name;
      this.age = age;
    }
  }

  public partial class Window1 : System.Windows.Window {
    public Window1() {
      InitializeComponent();
      this.birthdayButton.Click += birthdayButton_Click;
    }

    void birthdayButton_Click(object sender, RoutedEventArgs e) {
      Person person = (Person)this.FindResource("Tom");
      person.Age = person.Age+1;
      BindingOperations.GetBindingExpression(ageTextBox, TextBox.TextProperty).UpdateTarget();

      Console.WriteLine(person.Name);
      Console.WriteLine(person.Age);
    }
  }
}

   
    
     


Property changed callback

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="Window1" Height="300" Width="300">

    <StackPanel>
      <TextBox x:Name="uv" Text="{Binding Path=UserValue, UpdateSourceTrigger=PropertyChanged}" 
               />

    </StackPanel>

</Window>


//File:Window1.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
        }
        private static void UserValue_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Window1 window1 = d as Window1;

            if (window1 != null)
            {                
                window1.uv.Foreground = Brushes.SeaGreen;
            }
        }
    }
}