<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MultiBrowse" Height="600" Width="800" Loaded="OnLoaded"> <StackPanel Name="Stack" Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Button Content="New Window" Click="NewWindowHandler" /> <TextBox Name="newLocation" Width="500" /> <Button Content="GO!" Click="Browse" /> </StackPanel> <Frame Name="placeHolder" Width="800" Height="550"></Frame> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Threading; using System.Threading; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() : base() { InitializeComponent(); } private void OnLoaded(object sender, RoutedEventArgs e) { placeHolder.Source = new Uri("http://www.kutayzorlu.com/java2s/com"); } private void Browse(object sender, RoutedEventArgs e) { placeHolder.Source = new Uri(newLocation.Text); } private void NewWindowHandler(object sender, RoutedEventArgs e) { Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint)); newWindowThread.SetApartmentState(ApartmentState.STA); newWindowThread.IsBackground = true; newWindowThread.Start(); } private void ThreadStartingPoint() { Window1 tempWindow = new Window1(); tempWindow.Show(); System.Windows.Threading.Dispatcher.Run(); } } }
Author: coder
Use a Thumb to resize a Canvas control by responding to the DragDelta event.
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Thumb_wcp.Pane1"> <Canvas Width="100" Height="100" Name="myCanvasStretch"> <TextBox Name="changes" Width="{Binding ElementName=myCanvasStretch,Path=Width}" Height="{Binding ElementName=myCanvasStretch,Path=Height}" Text="Size: 100, 100"/> <Thumb Name="myThumb" Canvas.Left="80" Canvas.Top="80" Background="Blue" Width="20" Height="20" DragDelta="onDragDelta"/> </Canvas> </Canvas> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Documents; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Data; using System.Windows.Media; namespace Thumb_wcp { public partial class Pane1 : Canvas { void onDragDelta(object sender, DragDeltaEventArgs e) { double yadjust = myCanvasStretch.Height + e.VerticalChange; double xadjust = myCanvasStretch.Width + e.HorizontalChange; if ((xadjust >= 0) && (yadjust >= 0)) { myCanvasStretch.Width = xadjust; myCanvasStretch.Height = yadjust; Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) + e.HorizontalChange); Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) + e.VerticalChange); Console.WriteLine(myCanvasStretch.Width); Console.WriteLine(myCanvasStretch.Height); } } } }
Thumb DragStarted and DragCompleted event handler
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Thumb_wcp.Pane1"> <StackPanel> <Thumb Name="myThumb" Background="Blue" Width="20" Height="20" DragStarted="onDragStarted" DragCompleted="onDragCompleted"/> </StackPanel> </Canvas> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Documents; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Data; using System.Windows.Media; namespace Thumb_wcp { public partial class Pane1 : Canvas { void onDragStarted(object sender, DragStartedEventArgs e) { myThumb.Background = Brushes.Orange; } void onDragCompleted(object sender, DragCompletedEventArgs e) { myThumb.Background = Brushes.Blue; } } }
Check Whether You Are Running on the UI Thread
<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="120" Width="364"> <StackPanel> <Button Click="ButtonTrue_Click">UI Thread</Button> <Button Click="ButtonFalse_Click">Non-UI Thread</Button> <TextBlock x:Name="txtResult"/> </StackPanel> </Window> //File:Window.xaml.cs using System.Windows; using System.Windows.Threading; namespace WpfApplication1 { public partial class Window1 : Window { private delegate void CheckAccessDelegate(); private delegate void SetResultTextDelegate(string result); public Window1() { InitializeComponent(); } private void ButtonTrue_Click(object sender, RoutedEventArgs e) { CheckAccess(); } private void ButtonFalse_Click(object sender, RoutedEventArgs e) { CheckAccessDelegate del = new CheckAccessDelegate(CheckAccess); del.BeginInvoke(null, null); } private void CheckAccess() { if(txtResult.Dispatcher.CheckAccess()) { txtResult.Text = "True"; }else{ txtResult.Dispatcher.BeginInvoke(DispatcherPriority.Normal,new SetResultTextDelegate(SetResultText),"False"); } } private void SetResultText(string result) { txtResult.Text = result; } } }
Ensure That You Are Running on the UI Thread
<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="100" Width="300"> <StackPanel> <Button Click="ButtonTrue_Click">UI Thread</Button> <Button Click="ButtonFalse_Click">Non-UI Thread</Button> </StackPanel> </Window> //File:Window.xaml.cs using System.Windows; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void ButtonTrue_Click(object sender, RoutedEventArgs e) { this.Dispatcher.VerifyAccess(); } private void ButtonFalse_Click(object sender, RoutedEventArgs e) { VerifyAccessDelegate del = new VerifyAccessDelegate(VerifyAccess); del.BeginInvoke(null, null); } private delegate void VerifyAccessDelegate(); private void VerifyAccess() { this.Dispatcher.VerifyAccess(); } } }
Execute a Method Asynchronously Using the Dispatcher Queue
<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" Width="220" Height="104" > <StackPanel Orientation="Vertical"> <Button Click="StartStop_Click" Name="btnStartStop">Start</Button> <StackPanel Orientation="Horizontal"> <TextBlock>Biggest Prime Found:</TextBlock> <TextBlock Name="txtNumber"/> </StackPanel> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Threading; namespace WpfApplication1 { public partial class Window1 : Window { private bool continueCalculating = false; public Window1() : base() { InitializeComponent(); } private void StartStop_Click(object sender, RoutedEventArgs e) { if(continueCalculating) { continueCalculating = false; btnStartStop.Content = "Start"; } else { continueCalculating = true; btnStartStop.Content = "Stop"; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,new Action<int>(UpdateNumber), 3); } } public void UpdateNumber(int current) { txtNumber.Text = current.ToString(); if(continueCalculating) { this.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,new Action<int>(UpdateNumber), current + 2); } } } }
ThreadPool.QueueUserWorkItem
<Window x:Class="DispatcherExamples.MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Use BeginInvoke" Height="300" Width="300" > <Grid> <StackPanel> <Button x:Name="useBeginInvokeButton" Content="Use BeginInvoke" /> </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.Windows.Threading; using System.Threading; namespace DispatcherExamples { public partial class MyWindow : System.Windows.Window { public MyWindow() { InitializeComponent(); useBeginInvokeButton.Click += new RoutedEventHandler(useBeginInvokeButton_Click); } void useBeginInvokeButton_Click(object sender, RoutedEventArgs e) { ThreadPool.QueueUserWorkItem(delegate { MyDelegateType methodForUiThread = delegate { this.Background = new SolidColorBrush(Color.FromRgb(3,2, 1)); }; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, methodForUiThread); }); } public delegate void MyDelegateType(); } }