Search for an element by using Panel.FindName()


   
  

<StackPanel Name="root"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.FEFindName">
  <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
      <Style TargetType="{x:Type TextBlock}">
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="FontSize" Value="20"/>
      </Style>
  </StackPanel.Resources>
  <Button Click="Find">Find element with the ID "dog" and change color</Button>
  <StackPanel Name="stackPanel">
    <TextBlock Name="cat">Cat</TextBlock>
    <TextBlock Name="dog">Dog</TextBlock>
    <TextBlock Name="fish">Fish</TextBlock>    
  </StackPanel>
</StackPanel>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class FEFindName {
        void Find(object sender, RoutedEventArgs e)
        {
            object wantedNode = stackPanel.FindName("dog");
            if (wantedNode is TextBlock)
            {
                TextBlock wantedChild = wantedNode as TextBlock;
                wantedChild.Foreground = Brushes.Blue;
            }
        }
    }
}

   
    
     


Get all children from a Panel


   
  
<StackPanel Name="root"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.DPClearValue">
    <StackPanel.Resources>
      <Style TargetType="Button">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
      <Style TargetType="Ellipse">
        <Setter Property="Height" Value="50"/>
        <Setter Property="Width" Value="50"/>
        <Setter Property="Fill" Value="Black"/>
      </Style>
      <Style TargetType="Rectangle">
        <Setter Property="Height" Value="50"/>
        <Setter Property="Width" Value="50"/>
        <Setter Property="Fill" Value="Blue"/>
      </Style>
      <Style TargetType="Polygon">
        <Setter Property="Points" Value="10,60 60,60 60,10"/>
        <Setter Property="Fill" Value="Blue"/>
      </Style>
      <Style x:Key="ShapeStyle" TargetType="Shape">
        <Setter Property="Fill" Value="Red"/>
      </Style>
    </StackPanel.Resources>
  <DockPanel Name="myDockPanel">
    <Ellipse Height="100"  Width="100" Style="{StaticResource ShapeStyle}"/>
    <Rectangle Height="100" Width="100"  Style="{StaticResource ShapeStyle}" />
    <Polygon Points="10,110 110,110 110,10" Style="{StaticResource ShapeStyle}"/>
  </DockPanel>
    <Button Name="RedButton" Click="MakeEverythingRed">Make everything red</Button>
    <Button Name="ClearButton" Click="RestoreDefaultProperties">
  Clear local values
  </Button>

</StackPanel>

//File:Window.xaml.cs


using System.Windows;
using System.Collections;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace WpfApplication1 {
    public partial class DPClearValue {

        void RestoreDefaultProperties(object sender, RoutedEventArgs e)
        {
            UIElementCollection uic = myDockPanel.Children;
            foreach (Shape uie in uic)
            {
                LocalValueEnumerator locallySetProperties = uie.GetLocalValueEnumerator();
                while (locallySetProperties.MoveNext())
                {
                    DependencyProperty propertyToClear = (DependencyProperty)locallySetProperties.Current.Property;
                    if (!propertyToClear.ReadOnly) { uie.ClearValue(propertyToClear); }
                }
            }
        }
        void MakeEverythingRed(object sender, RoutedEventArgs e)
        {
            UIElementCollection uic = myDockPanel.Children;
            foreach (Shape uie in uic) {uie.Fill = new SolidColorBrush(Colors.Red);}
        }
    }
}

   
    
     


Insert to a Panel by index


   
  
<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.RoutedEventAddRemoveHandler" 
  Name="root">
    <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
    </StackPanel.Resources>
  <TextBlock Name="text1">Clicking the button below</TextBlock>
  <Button Name="b1" Click="MakeButton">Make new button and add handler to it</Button>
</StackPanel>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1 {
    public partial class RoutedEventAddRemoveHandler {
        
        void MakeButton(object sender, RoutedEventArgs e)
        {
            Button b2 = new Button();
            b2.Content = "New Button";
            root.Children.Insert(root.Children.Count, b2);
            DockPanel.SetDock(b2, Dock.Top);
            text1.Text = "click me...";
            b1.IsEnabled = false;
        }
    }
}

   
    
     


Find the index number of a newly added element within a panel, using the IndexOf method


   
  


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.IndexOfSample" 
  WindowTitle="UIElementCollection IndexOf Sample">
    <DockPanel Name="ParentElement">
        <TextBlock DockPanel.Dock="Top" Name="TxtDisplay"></TextBlock>

        <Button DockPanel.Dock="Top" Click="FindIndex">What is the Index Number
            of the Element Just Added?</Button>
        <DockPanel Name="MainDisplayPanel">
            <TextBlock DockPanel.Dock="Top">Text 1</TextBlock>
            <TextBlock DockPanel.Dock="Top">Text 2</TextBlock>
        </DockPanel>
    </DockPanel>
</Page>

//File:Window.xaml.cs

namespace WpfApplication1 {
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;

    public partial class IndexOfSample {


    void FindIndex(object sender, RoutedEventArgs e)
    {

      TextBlock newText = new TextBlock();
      MainDisplayPanel.Children.Add(newText);
      newText.Text = "New element # ";
      DockPanel.SetDock(newText,Dock.Top);
      TxtDisplay.Text = ""+MainDisplayPanel.Children.IndexOf(newText);
    }
  }
}

   
    
     


Panel is setting the data context to the scrollbar object


   
  

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myConverters ="clr-namespace:WpfApplication1" 
    Title="Simple Data Binding" Height="334" Width="288" 
    WindowStartupLocation="CenterScreen" 
    >
  <Window.Resources>
    <myConverters:MyDoubleConverter x:Key="DoubleConverter"/>
    <myConverters:MyColorConverter x:Key="ColorConverter"/>
  </Window.Resources>
    <StackPanel Width="250" DataContext = "{Binding ElementName=mySB}">
     <ScrollBar Orientation="Horizontal" Name="mySB" Maximum = "100" LargeChange="1" SmallChange="1"/>
     <TextBox Name="txtThumbValue" Text = "{Binding Path=Value, Converter={StaticResource DoubleConverter}}"/>
     <Button Content="Click" FontSize = "{Binding Path=Value}" Background= "{Binding Path=Value, Converter={StaticResource ColorConverter}}"/>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
using System.Collections.Generic;
namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();      
    }
  }

  class MyColorConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture)
    {
      double d = (double)value;
      byte v = (byte)d;

      Color color = new Color();
      color.A = 255;
      color.G = (byte) (155 + v);

      return new SolidColorBrush(color);
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture)
    {
      return value;
    }
  }

  class MyDoubleConverter : IValueConverter 
  {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      double v = (double)value;
      return (int)v;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      return value;
    } 
  }
}

   
    
     


Get Item At Index Position [0]


   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ElemCollMethods.Pane1"
    WindowTitle="UI Element Collection Methods Sample">
 <StackPanel>
    <TextBlock Name="txt" FontSize="16">UI Element Collection - Methods</TextBlock>

    <TabControl>
        <TabItem MouseLeftButtonUp="GetItem">
           <TabItem.Header>Get Item At Index Position [0]</TabItem.Header>
        </TabItem>
    </TabControl>
    <StackPanel Name="sp1"></StackPanel>

 </StackPanel>
</Page>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Input;

namespace ElemCollMethods
{

  public partial class Pane1 : Page
  {
    System.Windows.Controls.Button btn, btn1, btn2, btn3;


        void GetItem(object sender, RoutedEventArgs e)
        {
            TextBlock txt2 = new TextBlock();
            sp1.Children.Add(txt2);
            txt2.Text = "UIElement at Index position [0] is " + sp1.Children[0].ToString();
        }
  }
}

   
    
     


UIElement Count


   
  
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ElemCollMethods.Pane1"
    WindowTitle="UI Element Collection Methods Sample">
 <StackPanel>
    <TextBlock Name="txt" FontSize="16">UI Element Collection - Methods</TextBlock>

    <TabControl>
        <TabItem MouseLeftButtonUp="GetCount">
            <TabItem.Header>UIElement Count</TabItem.Header>
        </TabItem>
    </TabControl>
    <StackPanel Name="sp1"></StackPanel>

 </StackPanel>
</Page>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Input;

namespace ElemCollMethods
{

  public partial class Pane1 : Page
  {
    System.Windows.Controls.Button btn, btn1, btn2, btn3;



        void GetCount(object sender, RoutedEventArgs e)
        {
            TextBlock txt3 = new TextBlock();
            sp1.Children.Add(txt3);
            txt3.Text = "UIElement Count is equal to " + sp1.Children.Count.ToString();
        }
  }
}