DockPanel with TextBlock


   
      
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      WindowTitle="DockPanel Sample">

    <StackPanel>
        <TabControl MinHeight="500" MinWidth="400">
            <TabItem Header="DockPanel" IsSelected="true">
                <DockPanel>
                    <Border Height="25" Background="Red" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
                        <TextBlock Foreground="black">Dock = "Top"</TextBlock>
                    </Border>
                    <Border Height="25" Background="Blue" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
                        <TextBlock Foreground="black">Dock = "Top"</TextBlock>
                    </Border>
                    <Border Height="25" Background="Black" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Bottom">
                        <TextBlock Foreground="black">Dock = "Bottom"</TextBlock>
                    </Border>
                    <Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Left">
                        <TextBlock Foreground="black">Dock = "Left"</TextBlock>
                    </Border>
                    <Border Background="White" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Foreground="black">This content fills the remaining, unallocated space.</TextBlock>
                    </Border>
                </DockPanel>
            </TabItem>

        </TabControl>

    </StackPanel>

</Page>

   
    
    
    
    
    
     


MailDispatcher and NotifyIcon


   
  


<Window x:Class="ActivationSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ActivationSample"
    >
  <Grid>
    <ListBox Name="mailListBox" />
  </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.Forms;
using System.Windows.Threading;

namespace ActivationSample
{
    public partial class MainWindow : Window
    {
        private bool isActive;
        private MailDispatcher mailDispatcher = new MailDispatcher();
        private NotifyIcon newMailNotifyIcon = new NotifyIcon();

        public MainWindow()
        {
            InitializeComponent();

            this.mailDispatcher.MailDispatched += mailDispatcher_MailDispatched;
            this.mailDispatcher.Start();
           // this.newMailNotifyIcon.Icon = ActivationSample.Properties.Resources.NewMailIcon;            
            this.isActive = true;
            this.newMailNotifyIcon.Visible = false;

        }

        public void AddMailItem(string data)
        {
            this.mailListBox.Items.Add(data);
        }

        void mailDispatcher_MailDispatched(object sender, EventArgs e)
        {
            ((MainWindow)this).AddMailItem(DateTime.Now.ToString());
            if (!this.isActive &amp;&amp; this.newMailNotifyIcon.Visible == false)
            {
                this.newMailNotifyIcon.Visible = true;
            }
        }
//        void App_Deactivated(object sender, EventArgs e)
  //      {
    //        this.isActive = false;
      //  }

//        void App_Exit(object sender, ExitEventArgs e)
  //      {
    //        this.mailDispatcher.Stop();
      //  }        
    }

    public class MailDispatcher
    {
        DispatcherTimer timer;

        public event EventHandler MailDispatched;

        public void Start()
        {
            this.timer = new DispatcherTimer();
            timer.Tick += timer_Tick;
            timer.IsEnabled = true;
            timer.Interval = new TimeSpan(0, 0, 5);
        }

        public void Stop()
        {
            this.timer.IsEnabled = false;
            this.timer.Tick -= timer_Tick;
            this.timer = null;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (MailDispatched != null) MailDispatched(this, EventArgs.Empty);
        }
    }    
}

   
    
     


Using a DispatcherTimer


   
  

<Window x:Class="DispatcherExamples2.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DispatcherTimer" Height="300" Width="300">
    <Grid>
        
    </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;
using System.Windows.Threading;

namespace DispatcherExamples2
{
    partial class MyWindow : Window
    {
        DispatcherTimer dt = new DispatcherTimer();
        public MyWindow()
        {
            InitializeComponent();
            dt.Tick += dt_Tick;
            dt.Interval = TimeSpan.FromSeconds(2);
            dt.Start();
        }
        void dt_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            byte[] vals = new byte[3];
            rnd.NextBytes(vals);
            Color c = Color.FromRgb(vals[0], vals[1], vals[2]);
            this.Background = new SolidColorBrush(c);
        }
    }
}

   
    
     


Dispatcher Examples


   
  
<Window x:Class="DispatcherExamples.AvoidWait"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DispatcherExamples" Height="300" Width="300"
    >
    <Grid>
        
    </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;
using System.Windows.Threading;

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

        delegate void MyDelegateType();

        private void BackAndForth()
        {
            MyDelegateType work = delegate{
                DoWorkOnUIThread();
                DoTheMainWorkDone();
            };
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, work);
        }
        private void DoTheMainWorkDone()
        {
            Console.WriteLine("DoTheMainWorkDone");
        }

        private void DoWorkOnUIThread()
        {
            Console.WriteLine("DoWorkOnUIThread");
        }
    }
}

   
    
     


Use DispatcherTimer to change Dependency Property


   
  
<Window x:Name="winThis" x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Create a Read-Only Dependency Property" Height="300" Width="300">
    <Grid>
      <Viewbox>
        <TextBlock Text="{Binding ElementName=winThis, Path=Counter}" />
      </Viewbox>
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Threading;

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

            DispatcherTimer timer = 
                new DispatcherTimer(TimeSpan.FromSeconds(1), 
                                    DispatcherPriority.Normal, 
                                    delegate 
                                    {
                                        int newValue = Counter == int.MaxValue ? 0 : Counter + 1;
                                        SetValue(counterKey, newValue); 
                                    }, 
                                    Dispatcher);
        }

        public int Counter
        {
            get { return (int)GetValue(counterKey.DependencyProperty); }
        }

        private static readonly DependencyPropertyKey counterKey = 
            DependencyProperty.RegisterReadOnly("Counter", 
                                                typeof(int), 
                                                typeof(Window1), 
                                                new PropertyMetadata(0));
    }
}

   
    
     


Dispatcher.BeginInvoke with DispatcherPriority.Normal


   
  

<Window x:Class="DispatcherExamples.UseVerifyAccess"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DispatcherExamples" Height="300" Width="300"
    >
    <Grid>
        <StackPanel>
            <Button Content="Call from UI Thread" x:Name="fromUiButton" />
            <TextBlock x:Name="result" TextWrapping="Wrap" />
        </StackPanel>
    </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;
using System.Threading;
using System.Windows.Threading;

namespace DispatcherExamples
{
    public partial class UseVerifyAccess : System.Windows.Window
    {

        public UseVerifyAccess()
        {
            InitializeComponent();

            fromUiButton.Click += new RoutedEventHandler(fromUiButton_Click);
        }

        void fromUiButton_Click(object sender, RoutedEventArgs e)
        {
            string resultText;
            try
            {
                resultText = "Success";
            }
            catch (Exception x)
            {
                resultText = x.ToString();
            }
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                result.Text = resultText;
            });
        }
    }
}

   
    
     


DispatcherTimer and EventHandler


   
  


<Window x:Class="Holding.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FillingExample" Height="300" Width="300">
  <Canvas>
    <Ellipse Name="myEllipse" Height="100" Fill="Red">
      <Ellipse.Triggers>
        <EventTrigger RoutedEvent="Ellipse.Loaded">
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation BeginTime="0:0:2" Duration="0:0:5"
                  Storyboard.TargetProperty="(Ellipse.Width)"
                  From="10" To="300" FillBehavior="Stop" />

            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Ellipse.Triggers>
    </Ellipse>
  </Canvas>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Threading;
using System.Diagnostics;

namespace Holding
{
    public partial class Window2 : Window
    {
        DispatcherTimer t = new DispatcherTimer();
        DateTime start;
        public Window2()
        {
            InitializeComponent();
            t.Tick += new EventHandler(OnTimerTick);
            t.Interval = TimeSpan.FromSeconds(0.5);
            t.Start();
            start = DateTime.Now;
        }
        void OnTimerTick(object sender, EventArgs e)
        {
            TimeSpan elapsedTime = DateTime.Now - start;
            Debug.WriteLine(elapsedTime.ToString() + ": " + myEllipse.Width);
        }
    }
}