Thread Sleep in Button Click handler


   
  

<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" >
      text
    </TextBox>
    <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer"
            Click="cmdAnswer_Click" 
            Grid.Row="1">      
      Click
      </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">
      text
    </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;
        }

    }
}

   
    
     


WPF Threading


   
  

<Window x:Class="WPFThreading.BlockThread"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="UI Thread Blocker" Height="300" Width="250">
    <StackPanel>
      <Button Name="button1" Click="button1_click">Go to sleep</Button>
      <Button Name="button2" Click="button2_click">Try Me</Button>
      <TextBox Name="textbox1"/>
      <Label Name="UIThreadLabel"/>
      <Label Name="BackgroundThreadLabel"/>
    </StackPanel>
</Window>

//File:Window.xaml.cs

using System.Windows;

namespace WPFThreading
{
  public partial class BlockThread : System.Windows.Window
  {
    public BlockThread()
    {
      InitializeComponent();

      this.UIThreadLabel.Content = this.Dispatcher.Thread.ManagedThreadId;
      this.BackgroundThreadLabel.Content = "N/A";
    }

    private void button1_click(object sender, RoutedEventArgs e)
    {
      System.Threading.Thread.Sleep(5000);
      this.textbox1.Text = "Done Sleeping...";
    }

    private void button2_click(object sender, RoutedEventArgs e)
    {
      this.textbox1.Text = "Hello WPF";
    }

  }
}

   
    
     


Clear any formatting applied to the text with TextRange.ClearAllProperties();


   
  


<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="600" Width="800">
  <Grid>
    <RichTextBox x:Name="rtbTextContent" TextChanged="RichTextBox_TextChanged" />
  </Grid>
</Window>
//File:Window.xaml.cs
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 RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

            TextRange textRange = new TextRange(rtbTextContent.Document.ContentStart, rtbTextContent.Document.ContentEnd);
            rtbTextContent.TextChanged -= RichTextBox_TextChanged;
            textRange.ClearAllProperties();
            ApplyFormatting();
            rtbTextContent.TextChanged += RichTextBox_TextChanged;
        }

        private void ApplyFormatting()
        {
            TextPointer tp = rtbTextContent.Document.ContentStart;
            tp = FindNextString(tp);

            TextPointer textRangeEnd = tp.GetPositionAtOffset(1, LogicalDirection.Forward);

            TextRange tokenTextRange = new TextRange(tp, tp.GetPositionAtOffset(1, LogicalDirection.Forward));

            tokenTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
        }

        private TextPointer FindNextString(TextPointer tp)
        {
            char[] buffer = new char[1];
            tp.GetTextInRun(LogicalDirection.Forward, buffer, 0, 1);
            return tp;
        }
    }
}

   
    
     


Default overline decoration


   
 

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="TextDecorationExample.Window1"
  Title="TextDecoration Example"
  Width="720"
  Height="400">
  <StackPanel>
      <TextBlock 
        FontSize="24" 
        Width="180" 
        VerticalAlignment="Center" 
        TextDecorations="OverLine">The lazy dog</TextBlock>
  </StackPanel>
</Window>

   
     


Default baseline decoration


   
 

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="TextDecorationExample.Window1"
  Title="TextDecoration Example"
  Width="720"
  Height="400">
  <StackPanel>
      <TextBlock 
        FontSize="24" 
        Width="180" 
        VerticalAlignment="Center" 
        TextDecorations="Baseline">The lazy dog</TextBlock>
  </StackPanel>
</Window>

   
     


Default underline decoration


   
 

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="TextDecorationExample.Window1"
  Title="TextDecoration Example"
  Width="720"
  Height="400">
  <StackPanel>
      <TextBlock
        FontSize="24" 
        Width="180"
        VerticalAlignment="Center"
        TextDecorations="Underline">The lazy dog</TextBlock>
  </StackPanel>
</Window>

   
     


Fill the overline decoration with a linear gradient brush


   
 

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="TextDecorationExample.Window1"
  Title="TextDecoration Example"
  Width="720"
  Height="400">
  <StackPanel>
     <TextBlock FontSize="24" Width="180" VerticalAlignment="Center">The lazy dog
        <TextBlock.TextDecorations>
          <TextDecoration Location="OverLine" 
            PenThicknessUnit="FontRecommended">
            <TextDecoration.Pen>
              <Pen Thickness="3">
                <Pen.Brush>
                  <LinearGradientBrush 
                    StartPoint="0,0.5"  EndPoint="1,0.5">
                    <LinearGradientBrush.GradientStops>
                      <GradientStop Color="LimeGreen" Offset="0" />
                      <GradientStop Color="Yellow" Offset="1" />
                    </LinearGradientBrush.GradientStops>
                  </LinearGradientBrush>
                </Pen.Brush>
              </Pen>
            </TextDecoration.Pen>
            </TextDecoration>
        </TextBlock.TextDecorations>
      </TextBlock>
  </StackPanel>
</Window>