Get all children from a Panel : Panel « Windows Presentation Foundation « C# / C Sharp






Get all children from a Panel

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

   
    
  








Related examples in the same category

1.Add a control to a PanelAdd a control to a Panel
2.Remove Control from a PanelRemove Control from a Panel
3.Insert Button to a PanelInsert Button to a Panel
4.Add child controlAdd child control
5.Show Index of ControlsShow Index of Controls
6.Clear ControlsClear Controls
7.Contains Element?Contains Element?
8.UIElement CountUIElement Count
9.Get Item At Index Position [0]Get Item At Index Position [0]
10.Panel is setting the data context to the scrollbar objectPanel is setting the data context to the scrollbar object
11.Find the index number of a newly added element within a panel, using the IndexOf methodFind the index number of a newly added element within a panel, using the IndexOf method
12.Insert to a Panel by indexInsert to a Panel by index
13.Search for an element by using Panel.FindName()Search for an element by using Panel.FindName()