Specify Validation Rules for a Binding


















//File:Window.xaml.cs
using System.Globalization;
using System.Windows.Controls;

namespace WpfApplication1
{
public class PercentageRule : ValidationRule
{
public override ValidationResult Validate(object value,CultureInfo cultureInfo)
{
string stringValue = value as string;
if(!string.IsNullOrEmpty(stringValue))
{
double doubleValue;
if(double.TryParse(stringValue, out doubleValue))
{
if(doubleValue >= 0 && doubleValue <= 100) { return new ValidationResult(true, null); } } } return new ValidationResult(false, "Must be a number between 0 and 100"); } } } [/csharp]

Rotate


   
     
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
        <Canvas Height="200" Width="200">
          <Polyline Points="25,25 0,50 25,75 50,50 25,25 25,0" Stroke="Blue" StrokeThickness="10"
            Canvas.Left="75" Canvas.Top="50">
            <Polyline.RenderTransform>
              <RotateTransform CenterX="25" CenterY="50" Angle="45" />
            </Polyline.RenderTransform>
          </Polyline>
          
        
          <Polyline Points="25,25 0,50 25,75 50,50 25,25 25,0" Stroke="Blue" StrokeThickness="10"
            Opacity="0.25" Canvas.Left="75" Canvas.Top="50" />           
        </Canvas>

</Window>

   
    
    
    
    
     


Translate


   
     

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
        <Canvas Height="200" Width="200">
          <Polyline Points="25,25 0,50 25,75 50,50 25,25 25,0" Stroke="Blue" StrokeThickness="10"
            Canvas.Left="75" Canvas.Top="50">
            <Polyline.RenderTransform>
              <TranslateTransform X="50" Y="0" />
            </Polyline.RenderTransform>
          </Polyline>
          
          <!-- Shows the original position of the polyline. -->
          <Polyline Points="25,25 0,50 25,75 50,50 25,25 25,0" Stroke="Blue" StrokeThickness="10"
            Opacity="0.25" Canvas.Left="75" Canvas.Top="50" />            
        </Canvas>

</Window>

   
    
    
    
    
     


Gradient brushes within a DrawingBrush


   
     

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
      <Rectangle Width="50" Height="50" Grid.Row="2" Grid.Column="1">
        <Rectangle.Fill>
          <VisualBrush>
            <VisualBrush.Visual>
              <StackPanel Background="White">
                <Button Margin="1">A Button</Button>
                <Button Margin="1">Another Button</Button>
              </StackPanel>
            </VisualBrush.Visual>
          </VisualBrush>
        </Rectangle.Fill>
      </Rectangle>

</Window>

   
    
    
    
    
     


Print Visual Tree










//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.Collections;

namespace WPFTrees
{

public partial class Window1 : System.Windows.Window
{

public Window1()
{
InitializeComponent();
}

public void PrintVisualTree(Visual visual)
{
Console.WriteLine(visual.GetType().ToString()) ;

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++) { PrintVisualTree((Visual)VisualTreeHelper.GetChild(visual, i)); } } private void buttonPrint_Click(Object sender, RoutedEventArgs e) { PrintVisualTree(this.button1); } } } [/csharp]

Find enclosure component


   
   

<Window x:Class="VisualTree.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="VisualTree" Height="300" Width="300">
  <Grid>      
      <Button Click="MyClickEvent" Name="btnGo">
        <TextBox Width="173" Height="27" Name="txt">
        </TextBox>        
      </Button>      
    </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;

namespace VisualTree
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void MyClickEvent(object sender, RoutedEventArgs e)
        {
            object desiredNode = btnGo.FindName("txt");
            if (desiredNode is TextBox)
            {
                TextBox desiredChild = desiredNode as TextBox;
                desiredChild.Background = Brushes.Green;
            }
        }

    }
}

   
    
    
     


Logical Visual Tree Sample


   
   

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LogicalVisualTreeSample" Height="300" Width="300">
  <WrapPanel Name="rootPanel">
    <Button>_Click me</Button>
  </WrapPanel>
</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.Diagnostics;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
      DumpLogicalTree(rootPanel);
    }
    protected override void OnContentRendered(EventArgs e) {
      base.OnContentRendered(e);
      DumpVisualTree(rootPanel);
    }

    void DumpLogicalTree(object parent) {
      string typeName = parent.GetType().Name;
      string name = null;
      DependencyObject doParent = parent as DependencyObject;

      if( doParent != null ) {
        name = (string)(doParent.GetValue(FrameworkElement.NameProperty) ?? "");
      }
      else {
        name = parent.ToString();
      }

      Debug.WriteLine(typeName);
      Console.WriteLine(name);
      if( doParent == null ) { return; }

      foreach( object child in LogicalTreeHelper.GetChildren(doParent) ) {
        DumpLogicalTree(child);
      }
    }

    void DumpVisualTree(DependencyObject parent) {
      string typeName = parent.GetType().Name;
      string name = (string)(parent.GetValue(FrameworkElement.NameProperty) ?? "");
      
      Debug.WriteLine(typeName);
      Console.WriteLine(name);

      for( int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i ) {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        DumpVisualTree(child);
      }
    }

  }
}