Use DispatcherTimer to change Dependency Property

image_pdfimage_print


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

image_pdfimage_print


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

   
    
     


Using a DispatcherTimer

image_pdfimage_print


   
  

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

   
    
     


MailDispatcher and NotifyIcon

image_pdfimage_print


   
  


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

   
    
     


DockPanel with TextBlock

image_pdfimage_print


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

   
    
    
    
    
    
     


DockPanel and Grid

image_pdfimage_print


   
      

<Window x:Class="StackPanelDemo.DockandGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DockGridDemo" Height="446" Width="535">
    <Grid>
      <DockPanel MinHeight="50" MinWidth="50" LastChildFill="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1" Margin="10,10,22,21" Width="NaN" Height="NaN" Name="dockPanel1">
        <Button Name="button1" DockPanel.Dock="Top">I am the toolbar</Button>
        <Button Name="button2" DockPanel.Dock="Bottom">I am the status bar</Button>
        <Button Name="button3">I am the left toolbox</Button>
        <Button Name="button4" DockPanel.Dock="Right">I am the right toolbox</Button>
        <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="0.1*"  />
          <RowDefinition Height="0.8*" />
          <RowDefinition Height="0.1*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="0.1*" />
          <ColumnDefinition Width="0.7*" />
          <ColumnDefinition Width="0.1*" />
        </Grid.ColumnDefinitions>
          <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Orientation="Horizontal">
            <Label>Your Name:</Label>
            <TextBox>Enter Name Here</TextBox>
            <Button>...</Button>
          </StackPanel>
        </Grid>

      </DockPanel>
    </Grid>
  </Window>
  

   
    
    
    
    
    
     


Put a Menu and toolbar on the top with DockPanel

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="Fun with Panels!" Height="291" Width="529">

  <DockPanel LastChildFill ="True">
    <Menu DockPanel.Dock ="Top" HorizontalAlignment="Left" Background="White">
      <MenuItem Header="_File">
        <Separator/>
        <MenuItem Header ="_Exit" />
      </MenuItem>
    </Menu>
    <StatusBar Name="statBar" DockPanel.Dock ="Bottom" 
               VerticalAlignment="Bottom" Background="Beige" >
      <StatusBarItem>
        <TextBlock>Ready</TextBlock>
      </StatusBarItem>
    </StatusBar>

    <StackPanel Background="LightSteelBlue" Orientation ="Vertical" DockPanel.Dock ="Left">
      <Label Name="lblInstruction" FontSize="15">Information</Label>
      <Label Name="lblMake">Make</Label>
      <TextBox Name="txtMake"/>
      <Label Name="lblColor">Color</Label>
      <TextBox Name="txtColor"/>
      <Label Name="lblPetName">Name</Label>
      <TextBox Name="txtPetName"/>
      <Button Name="btnOK">OK</Button>
    </StackPanel>
  
    <TextBlock Background ="LemonChiffon" FontSize ="20">test</TextBlock>
  </DockPanel>
</Window>