Bind Stroke Thickness to Slider


   
   

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <Polyline Margin="0.5in, 1.5in, 0, 0" 
              Points="0 0, 500 25, 0 50"
              VerticalAlignment="Center"
              Stroke="Blue"
              StrokeThickness="{Binding ElementName=sliderThickness,Path=Value }"
    />
    <Label Content="_Thickness" />
    <Slider Name="sliderThickness"
                    Minimum="0"
                    Maximum="100"
                    Value="24" />

</StackPanel>

   
    
    
     


Bind RelativeSource's AncestorType's Path


   
   


<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            TextBlock.FontSize="12" >
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}, AncestorLevel=2},Path=Orientation}" />

    </StackPanel>



</StackPanel>

   
    
    
     


Bind RelativeSource's AncestorType


   
   
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            TextBlock.FontSize="12" >
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock Text="This TextBlock is inside a StackPanel with " />
        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Orientation}" />
        <TextBlock Text=" orientation" />
    </StackPanel>


</StackPanel>

   
    
    
     


Two level path binding


   
   


<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Label Content="Angle" />
    <ScrollBar Name="angle" Orientation="Horizontal" Value="0" Minimum="0" Maximum="360" />
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=angle, Path=Value}" />

    <Label Content="CenterX" />
    <ScrollBar Name="xcenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" /> 
    <TextBlock HorizontalAlignment="Center" Margin="12" Text="{Binding ElementName=xcenter, Path=Value}" />

    <Label Content="CenterY" />
    <ScrollBar Name="ycenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" />
    <TextBlock HorizontalAlignment="Center" Margin="12" Text="{Binding ElementName=ycenter, Path=Value}" />

    <Canvas>

        <Button Name="btn" Content="Button" Canvas.Left="100" Canvas.Top="100">
            <Button.RenderTransform>
                <RotateTransform
                    Angle="{Binding ElementName=angle, Path=Value}"
                    CenterX="{Binding ElementName=xcenter, Path=Value}"
                    CenterY="{Binding ElementName=ycenter, Path=Value}" />
            </Button.RenderTransform>
        </Button>

        <StackPanel>
            <TextBlock Text="{Binding ElementName=btn, Path=ActualWidth}" />
            <TextBlock Text="::" />
            <TextBlock Text="{Binding ElementName=btn, Path=ActualHeight}" />
        </StackPanel>
    </Canvas>
</StackPanel>

   
    
    
     


Bind to Window itself


   
   

<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SizingPrecedenceSampleCSharp.SizingWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Title="Sizing Window" Name="window">
  <StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>WindowState:</Label>
      <Label Content="{Binding ElementName=window,Path=WindowState}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MinWidth:</Label>
      <Label Content="{Binding ElementName=window,Path=MinWidth}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MinHeight:</Label>
      <Label Content="{Binding ElementName=window,Path=MinHeight}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>SizeToContent:</Label>
      <Label Content="{Binding ElementName=window,Path=SizeToContent}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MaxWidth:</Label>
      <Label Content="{Binding ElementName=window,Path=MaxWidth}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MaxHeight:</Label>
      <Label Content="{Binding ElementName=window,Path=MaxHeight}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Width:</Label>
      <Label Content="{Binding ElementName=window,Path=Width}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Height:</Label>
      <Label Content="{Binding ElementName=window,Path=Height}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Actual Width:</Label>
      <Label Content="{Binding ElementName=window,Path=ActualWidth}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Actual Height:</Label>
      <Label Content="{Binding ElementName=window,Path=ActualHeight}"/>
    </StackPanel>
  </StackPanel>
</Window>

   
    
    
     


Bind property of one instantiated control


   
   

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="460" Height="200"
  Title="Binding the Properties of Two Controls">
    <DockPanel>
      <TextBlock>Choose a Color:</TextBlock>
      <ComboBox Name="myComboBox" SelectedIndex="0">
        <ComboBoxItem>Green</ComboBoxItem>
        <ComboBoxItem>Blue</ComboBoxItem>
        <ComboBoxItem>Red</ComboBoxItem>
      </ComboBox>
      <Canvas>
        <Canvas.Background>
          <Binding ElementName="myComboBox" Path="SelectedItem.Content"/>
        </Canvas.Background>
      </Canvas>
    </DockPanel>
</Window>

   
    
    
     


Unblock Thread with BackgroundWorker


   
  
<Window x:Class="WPFThreading.UnblockThreadTwo"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="UI Thread Blocker" Height="275" Width="225">
  <Border Width="200" Height="225" BorderBrush="Black" 
    BorderThickness="1" Margin="4">
    <StackPanel>

      <Label>Simulate Long-Running Process</Label>
      <Button Name="button1" Click="button1_click">Go to sleep</Button>

      <Label>Will I respond?</Label>
      <Button Name="button2" Click="button2_click">Try Me</Button>

      <Label>Output Messages</Label>
      <TextBox Name="textbox1"/>

      <Label/>

      <StackPanel Orientation="Horizontal">
        <Label>UI thread:</Label>
        <Label Name="UIThreadLabel"></Label>
      </StackPanel>

      <StackPanel Orientation="Horizontal">
        <Label>BG thread:</Label>
        <Label Name="BackgroundThreadLabel"></Label>
      </StackPanel>

    </StackPanel>
  </Border>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System.ComponentModel;
using System.Threading;
using System.Windows.Threading;

namespace WPFThreading
{
  
  public partial class UnblockThreadTwo : System.Windows.Window
  {

    private BackgroundWorker worker;

    private delegate void SimpleDelegate();

    public UnblockThreadTwo()
    {
      InitializeComponent();

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

    private void button1_click(object sender, RoutedEventArgs e)
    {
      worker = new BackgroundWorker();

      worker.DoWork += new DoWorkEventHandler(RunOnBGThread);

      worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGThreadWorkDone);

      worker.RunWorkerAsync();
    }

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

    private void LongRunningProcess()
    {

      SimpleDelegate del1 = delegate(){ this.BackgroundThreadLabel.Content = Thread.CurrentThread.ManagedThreadId; };
      this.Dispatcher.BeginInvoke(DispatcherPriority.Send, del1);

      Thread.Sleep(5000);
    }

    private void RunOnBGThread(object sender, DoWorkEventArgs e)
    {
      LongRunningProcess();
    }

    private void BGThreadWorkDone(object sender,RunWorkerCompletedEventArgs e)
    {
       this.textbox1.Text = "Done Sleeping..";
    }

  }
}