About Dialog with Tree Walking







1
2
3
4
5
6
7
8
9






text


//File:Window.xaml.cs

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;

public partial class AboutDialog : Window
{
public AboutDialog()
{
InitializeComponent();
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
PrintVisualTree(this);
}
void PrintVisualTree(DependencyObject obj)
{
Debug.WriteLine(obj);
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { PrintVisualTree(VisualTreeHelper.GetChild(obj,i)); } } } [/csharp]

Point Hit Test with VisualTreeHelper.HitTest


   
   
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d"
  x:Class="InputExamples.PointHitTest" 
  Width="640" Height="480">

  <StackPanel.Resources>
    <Storyboard x:Key="OnLoaded"/>
  </StackPanel.Resources>
  <StackPanel.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
      <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>
    </EventTrigger>
  </StackPanel.Triggers>
  <Rectangle Fill="Red" Margin="20" Width="200" Height="200" x:Name="RectangleArea"/>
</StackPanel>
//File:Window.xaml.cs

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace InputExamples
{
  public partial class PointHitTest
  {
    public PointHitTest()
    {
      this.InitializeComponent();
    }
    
    protected override void OnInitialized(EventArgs e)
    {
      base.OnInitialized(e);
      CompositionTarget.Rendering += this.CompositionTarget_Rendering;
    }

    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        Point position = Mouse.GetPosition(RectangleArea);
      VisualTreeHelper.HitTest(RectangleArea,null,new HitTestResultCallback(HitTestResultHandler),
              new PointHitTestParameters(position)
      );
    }
    
    public HitTestResultBehavior HitTestResultHandler(HitTestResult result)
    {
      PointHitTestResult hitResult = (PointHitTestResult)result;
      Console.WriteLine(((FrameworkElement)hitResult.VisualHit).Name);
      Console.WriteLine(hitResult.PointHit.ToString());
         return HitTestResultBehavior.Continue;
    }
  }
}

   
    
    
     


Find inner visual element


   
   

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

    }
}

   
    
    
     


Print Visual(Canvas)


   
   
<Window x:Class="Printing.PrintVisual"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="PrintVisual" Height="259" Width="282">
    <StackPanel>
      <Canvas Name="canvas">
        <TextBlock Canvas.Top="50" Canvas.Left="20" FontSize="25" FontWeight="Bold">Hello There</TextBlock>

        <Path Fill="Yellow" Stroke="Blue" Margin="5" Canvas.Top="10" Canvas.Left="10" >
          <Path.Data>
            <GeometryGroup>
              <RectangleGeometry Rect="0 0 100 100"></RectangleGeometry>
              <EllipseGeometry Center="50 50" RadiusX="35" RadiusY="25"></EllipseGeometry>
            </GeometryGroup>
          </Path.Data>
        </Path>
      </Canvas>
      <Button Grid.Row="3" Click="cmdPrint_Click">Print</Button>
    </StackPanel>
</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 Printing
{
    public partial class PrintVisual : System.Windows.Window
    {

        public PrintVisual()
        {
            InitializeComponent();
        }

        private void cmdPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            
            if (printDialog.ShowDialog() == true)
            {             
                double zoom = 30;                 

                canvas.Background = Brushes.LightSteelBlue;                                    
                    
                canvas.LayoutTransform = new ScaleTransform(zoom / 100, zoom / 100);

                Size pageSize = new Size(printDialog.PrintableAreaWidth - 20, printDialog.PrintableAreaHeight - 20);
                    
                canvas.Measure(pageSize);
                canvas.Arrange(new Rect(10, 10, pageSize.Width, pageSize.Height));

                printDialog.PrintVisual(canvas, "A Scaled Drawing");

                canvas.Background = null;
                canvas.LayoutTransform = null;                        
            }
        }
    }
}

   
    
    
     


Change control background in mouse enter and leave


   
   


<Window x:Class="Styles.EventSetter"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EventSetter" Height="300" Width="300">
  <Window.Resources>
    <Style x:Key="MouseOverHighlightStyle">
      <Setter Property="TextBlock.Padding" Value="5"/>
      <EventSetter Event="FrameworkElement.MouseEnter" Handler="element_MouseEnter" />
      <EventSetter Event="FrameworkElement.MouseLeave" Handler="element_MouseLeave" />
    </Style>
  </Window.Resources>
  
  <StackPanel>
    <TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock>
    <TextBlock Padding="5">asdf</TextBlock>
    <TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock>
  </StackPanel>
</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 Styles
{
    public partial class EventSetter : System.Windows.Window
    {
        public EventSetter()
        {
            InitializeComponent();
        }
        private void element_MouseEnter(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).Background = new SolidColorBrush(Colors.LightGoldenrodYellow);
        }
        private void element_MouseLeave(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).Background = null;
        }
    }
}

   
    
    
     


Changing graphical elements


   
   

<Window x:Class="ChangeItem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Change Item">

  <Canvas x:Name="mainCanvas">
    <Ellipse Canvas.Left="10" Canvas.Top="30" Fill="Indigo" Width="40" Height="20" />
    <Ellipse Canvas.Left="20" Canvas.Top="40" Fill="Blue" Width="40" Height="20" />
    <Ellipse Canvas.Left="30" Canvas.Top="50" Fill="Cyan" Width="40" Height="20" />
    <Ellipse Canvas.Left="40" Canvas.Top="60" Fill="LightGreen" Width="40" Height="20" />
    <Ellipse Canvas.Left="50" Canvas.Top="70" Fill="Yellow" Width="40" Height="20" />
  </Canvas>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Shapes;

namespace ChangeItem
{
    public partial class MainWindow : Window
    {
        public MainWindow(): base()
        {
            InitializeComponent();
            mainCanvas.MouseLeftButtonDown += OnClick;
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            Ellipse r = e.Source as Ellipse;
            if (r != null)
            {
                r.Width += 10;
            }
        }
    }
}

   
    
    
     


Change the Visibility property of a UIElement.


   
   

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Visibility_Layout_Samp.Window1"
    Title="UIELement.Visibility Sample">
      <DockPanel>
            <StackPanel DockPanel.Dock="Left">
                <Button Name="btn1" Height="25" Click="contentVis">Visibility="Visible"</Button>
                <Button Name="btn2" Height="25" Click="contentHid">Visibility="Hidden"</Button>
                <Button Name="btn3" Height="25" Click="contentCol">Visibility="Collapsed"</Button> 
            </StackPanel>    
            <StackPanel HorizontalAlignment="Center">
                <TextBox Name="tb1" Width="100" Height="50">A TextBox</TextBox>
                <TextBlock Name="txt1" TextWrapping="Wrap" FontSize="14"/>
            </StackPanel>
        </DockPanel>
</Window>

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

namespace Visibility_Layout_Samp
{
  public partial class Window1 : Window
  {
        public void contentVis(object sender, RoutedEventArgs e)
        {
            tb1.Visibility = System.Windows.Visibility.Visible;
            txt1.Text = "Visibility is now set to Visible.";
        }

        public void contentHid(object sender, RoutedEventArgs e)
        {
            tb1.Visibility = System.Windows.Visibility.Hidden;
            txt1.Text = "Visibility is now set to Hidden.";
        }

        public void contentCol(object sender, RoutedEventArgs e)
        {
            tb1.Visibility = System.Windows.Visibility.Collapsed;
            txt1.Text = "Visibility is now set to Collapsed.";
        }
    }
}