locked
how to find a specific control? RRS feed

  • Question

  •  

    how to find a specific control?

    there is grid control on silverlight page. grid control contain many border controls and rectangle controls.

    one of the border control contain stack panels and one of the stack panel contain hyperlink button.

    how can i find? 

    Tuesday, September 14, 2010 12:01 PM

Answers

  • if you're adding these controls at design time in xaml, you can use the x:Name property, and then use that in code behind or element to element binding:

    <Grid>
        <Border>
            <Rectangle x:Name="MyRectangle" />
        </Border>
    </Grid>

    then in your usercontrol's code:

    private void DoSomething()
    {
        MyRectangle.Fill = whatever;
    }

    Tuesday, September 14, 2010 12:09 PM
  • You can use the VisualTreeHelper class to find elements. Use VisualTreeHelper.GetParent to find a parent control and VisualTreeHelper.GetChild to find a child Control.


    Example using this xaml: 


    <Canvas x:Name="lbItemListItemGrid" Width="110" Height="150">



                <!--The first element - index(0)-->                                                     

         <Image x:Name="iThumbnail" Source="{Binding Path=Thumbnail}" Visibility="{Binding                Path=Enclosure.LocalFullPath,Converter={StaticResource ThumbnailPrevConverter}}"/>


    <!--The 2nd element - index(1)-->      

         <Image x:Name="imagePreview"  Source="{Binding Path=Enclosure.LocalFullPath}" Visibility="{Binding      Path=Enclosure.LocalFullPath}"  Width="110" Height="110" />



    <MediaElement AutoPlay="True" CurrentStateChanged="videoPreview_CurrentStateChanged" IsMuted="True" Grid.Row="1" Grid.Column="0"  Width="110" Height="110" x:Name="videoPreview" Source="{Binding Path=Enclosure.LocalFullPath}"/>



            <Button Content="Find Element" x:Name="btnFind" Height="20"  Click="Find_Click" Canvas.Left="7"                  Canvas.Top="60">

             </Button>

           <Button Content="Another Button" x:Name="btnAnother"/>

    </Canvas>



    <Canvas x:Name="lbItemListItemGrid" Width="110" Height="150">
                                                                     
    <Image x:Name="iThumbnail" Source="{Binding Path=Thumbnail}" Visibility="{Binding Path=Enclosure.LocalFullPath,Converter={StaticResource ThumbnailPrevConverter}}"/>
    <Image x:Name="imagePreview"  Source="{Binding Path=Enclosure.LocalFullPath}" Visibility="{Binding Path=Enclosure.LocalFullPath}"  Width="110" Height="110" />
    <MediaElement AutoPlay="True" CurrentStateChanged="videoPreview_CurrentStateChanged" IsMuted="True" Grid.Row="1" Grid.Column="0"  Width="110" Height="110" x:Name="videoPreview" Source="{Binding Path=Enclosure.LocalFullPath}"/>
    <Viewbox Width="110" Height="110">
    <ContentControl Content="{Binding}"
    Margin="10,10,10,10"
    x:Name="enclosurePreview">
    <ContentControl.Resources>
    <Style TargetType="ContentControl">
    <Setter Property="Visibility" Value="Collapsed"/>
    </Style>
    </ContentControl.Resources>
    </ContentControl>
    </Viewbox>
    <Button Content="????????" x:Name="bDownloadAsync" Height="20"  Click="bDownloadAsync_Click" Canvas.Left="7" Canvas.Top="60">
    </Button>
    <ProgressBar x:Name="progDownload" Width="100" Height="20" Canvas.Left="5" Canvas.Top="61" Minimum="0" Maximum="100"
    Value="{Binding Path=DownloadedProgress}" >
    </ProgressBar>
    <TextBlock Text="{Binding Path=Title}" Canvas.Top="110"/>
    </Canvas>

    1. When the button(btnFind) is click it will get a reference to the child elements.

    private void Find_Click(object sender,RoutedEventArgs e)

    {

              Button btn=sender as Button;

              //Get the Parent Canvas 1st

              Canvas parentCanvas=VisualTreeHelper.GetParent(btn) as Canvas;


    //Gets the first element , the Image element named (iThumbnail)

              Image thumbnail=VisualTreeHelper.GetChild(parentCanvas,0);


    //For checking purposes use the following code

            var obj=VisualTreeHelper.GetChild(parentCanvas,4);

            var fE=(FrameworkElement)obj;

         

            //Check the type

    if (fE.GetType().Equals(typeof(Button))

           {

                   Button anotherButton=fE as Button;
           }

             
    }


    I hope you understand and helps you..

    Mark as answer if this one helps.

    Wednesday, September 15, 2010 2:16 AM
  • You can use the static method UIHelper.FindVisualDecendantElementExact(...) located at:

    http://goodiesforria.svn.sourceforge.net/viewvc/goodiesforria/trunk/Casimodo/Casimodo.Lib.Silverlight/Helpers/UIHelper.cs?view=markup

    Regards,

    Kasimier Buchcik


    Wednesday, September 15, 2010 11:02 PM

All replies

  • if you're adding these controls at design time in xaml, you can use the x:Name property, and then use that in code behind or element to element binding:

    <Grid>
        <Border>
            <Rectangle x:Name="MyRectangle" />
        </Border>
    </Grid>

    then in your usercontrol's code:

    private void DoSomething()
    {
        MyRectangle.Fill = whatever;
    }

    Tuesday, September 14, 2010 12:09 PM
  • You can use the VisualTreeHelper class to find elements. Use VisualTreeHelper.GetParent to find a parent control and VisualTreeHelper.GetChild to find a child Control.


    Example using this xaml: 


    <Canvas x:Name="lbItemListItemGrid" Width="110" Height="150">



                <!--The first element - index(0)-->                                                     

         <Image x:Name="iThumbnail" Source="{Binding Path=Thumbnail}" Visibility="{Binding                Path=Enclosure.LocalFullPath,Converter={StaticResource ThumbnailPrevConverter}}"/>


    <!--The 2nd element - index(1)-->      

         <Image x:Name="imagePreview"  Source="{Binding Path=Enclosure.LocalFullPath}" Visibility="{Binding      Path=Enclosure.LocalFullPath}"  Width="110" Height="110" />



    <MediaElement AutoPlay="True" CurrentStateChanged="videoPreview_CurrentStateChanged" IsMuted="True" Grid.Row="1" Grid.Column="0"  Width="110" Height="110" x:Name="videoPreview" Source="{Binding Path=Enclosure.LocalFullPath}"/>



            <Button Content="Find Element" x:Name="btnFind" Height="20"  Click="Find_Click" Canvas.Left="7"                  Canvas.Top="60">

             </Button>

           <Button Content="Another Button" x:Name="btnAnother"/>

    </Canvas>



    <Canvas x:Name="lbItemListItemGrid" Width="110" Height="150">
                                                                     
    <Image x:Name="iThumbnail" Source="{Binding Path=Thumbnail}" Visibility="{Binding Path=Enclosure.LocalFullPath,Converter={StaticResource ThumbnailPrevConverter}}"/>
    <Image x:Name="imagePreview"  Source="{Binding Path=Enclosure.LocalFullPath}" Visibility="{Binding Path=Enclosure.LocalFullPath}"  Width="110" Height="110" />
    <MediaElement AutoPlay="True" CurrentStateChanged="videoPreview_CurrentStateChanged" IsMuted="True" Grid.Row="1" Grid.Column="0"  Width="110" Height="110" x:Name="videoPreview" Source="{Binding Path=Enclosure.LocalFullPath}"/>
    <Viewbox Width="110" Height="110">
    <ContentControl Content="{Binding}"
    Margin="10,10,10,10"
    x:Name="enclosurePreview">
    <ContentControl.Resources>
    <Style TargetType="ContentControl">
    <Setter Property="Visibility" Value="Collapsed"/>
    </Style>
    </ContentControl.Resources>
    </ContentControl>
    </Viewbox>
    <Button Content="????????" x:Name="bDownloadAsync" Height="20"  Click="bDownloadAsync_Click" Canvas.Left="7" Canvas.Top="60">
    </Button>
    <ProgressBar x:Name="progDownload" Width="100" Height="20" Canvas.Left="5" Canvas.Top="61" Minimum="0" Maximum="100"
    Value="{Binding Path=DownloadedProgress}" >
    </ProgressBar>
    <TextBlock Text="{Binding Path=Title}" Canvas.Top="110"/>
    </Canvas>

    1. When the button(btnFind) is click it will get a reference to the child elements.

    private void Find_Click(object sender,RoutedEventArgs e)

    {

              Button btn=sender as Button;

              //Get the Parent Canvas 1st

              Canvas parentCanvas=VisualTreeHelper.GetParent(btn) as Canvas;


    //Gets the first element , the Image element named (iThumbnail)

              Image thumbnail=VisualTreeHelper.GetChild(parentCanvas,0);


    //For checking purposes use the following code

            var obj=VisualTreeHelper.GetChild(parentCanvas,4);

            var fE=(FrameworkElement)obj;

         

            //Check the type

    if (fE.GetType().Equals(typeof(Button))

           {

                   Button anotherButton=fE as Button;
           }

             
    }


    I hope you understand and helps you..

    Mark as answer if this one helps.

    Wednesday, September 15, 2010 2:16 AM
  • You can use the static method UIHelper.FindVisualDecendantElementExact(...) located at:

    http://goodiesforria.svn.sourceforge.net/viewvc/goodiesforria/trunk/Casimodo/Casimodo.Lib.Silverlight/Helpers/UIHelper.cs?view=markup

    Regards,

    Kasimier Buchcik


    Wednesday, September 15, 2010 11:02 PM