locked
How to find control from gridview second item. RRS feed

  • Question

  • Hello,

    How can I find control from the second item inside gridview.

    My gridview is bound to datasource which contains multiples items, It has datatemplate containing two textbox and I need to find the refrence of textbox which is in gridview second item.

    I am using following code to find the child element in the gridview but it only returns the refence of first item textbox.I also tried using for each but in that also I am getting reference of first textbox on every iteration.

    Code:

     public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
            {
                if (parent == null) return null;
                T foundChild = null;
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
    
                    if (childType == null)
                    {
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            foundChild = (T)child;
                            break;
                        }
    
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
    
                return foundChild;
            }

    Thanks in advance.


    • Edited by zee_patel Monday, February 3, 2014 7:41 AM
    Monday, February 3, 2014 7:40 AM

All replies

  • How are you calling this? If you step through this in the debugger what path does it take and where does it set foundChild?

    From a quick read through the obvious way it'll find the first child is if childName is not passed in. Is it?

    What is your goal in finding this TextBox? Can you achieve that goal through data binding rather than pushing to the TextBox?

    --Rob

    Tuesday, February 4, 2014 1:53 AM
    Moderator
  • Hi Zee_patel,

    This code should not be any problem and may I know did you named the second TextBox?

    For instance I have two TextBox in the DataTemplate of GirdView (x:Name = gv), then I have to wrote:

    var k = FindChild<TextBox>(gv, "textBox2");

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, February 4, 2014 2:04 AM
    Moderator
  • Hi Jamles,

    Thanks for your reply. Your suggestion is correct , but what I want is the instance of first textbox which is in the gridview second item and not the second textbox which is in the gridview first items.

    I tried below code ,but at some items its gives null references . I tested  data binding and it is correct , so I can't figure out why I am getting null reference at particular item.

      foreach (var item in myGridView.Items)
                {
    var gridViewItem = myGridView.ItemContainerGenerator.ContainerFromIndex(item.No) as GridViewItem;
    var tb = FindChild<TextBox>(gridViewItem, "txtFirst");
    }

    Tuesday, February 4, 2014 5:48 AM
  • Hi Zee,

    Please answer the questions I asked in my previous response.

    Beyond that, what does the Xaml you are searching look like? Can you please provide a minimal sample demonstrating the problem?

    Thanks,
       Rob

    Tuesday, February 4, 2014 7:04 AM
    Moderator
  • Hi Rob,

    This what xaml look like...there is also second textbox (and also some other controls) but just for saving lines I removed it.

    what I need is that I need to get  the instance txtFirst textbox on second item(i.e. on second image)

    GridView x:Name="myGridView"   
                      ItemsSource="{Binding Mode=OneWay, Source={StaticResource fileSource}}"  
                      SelectionMode="None"   
                      Background="{StaticResource ApplicationPageBackgroundThemeBrush}"  ItemContainerStyle="{StaticResource GridViewItemStyle1}"  >
                <GridView.ItemTemplate>
                    <DataTemplate>
                    <Grid x:Name="OuterGrid" Width="{Binding GridWidth}" Height="{Binding GridHeight}" PointerPressed="Grid_PointerPressed_1">
                        <Grid x:Name="innerGrid" LayoutUpdated="innerGrid_LayoutUpdated" VerticalAlignment="Center"    HorizontalAlignment="Center" >
                            <ScrollViewer
                                    ZoomMode="Enabled" 
                                    HorizontalScrollBarVisibility="Disabled"
                                    VerticalScrollBarVisibility="Auto" 
                                    MinZoomFactor="1"            
                                    MaxZoomFactor="3.5" Grid.Column="1">
                                <Grid Grid.Column="1">
                                    <Image x:Name="imgImage" Source="{Binding Image}" Width="{Binding Width}"  VerticalAlignment="Center"     Height="{Binding Height}" Margin="0"  HorizontalAlignment="Center" />
    
                                    <Canvas>
                                        <Grid x:Name="grdFirstTextBox"  Canvas.Left="100"  Canvas.Top="300"  Visibility="Collapsed"  Width="200"  ManipulationMode="TranslateInertia" ManipulationDelta="grdTextBox_ManipulationDelta_1" ManipulationInertiaStarting="grdTextBox_ManipulationInertiaStarting_1">
                                            <Thumb x:Name="ThumbBottomRightText" Background="Gray" Height="10" Width="10" HorizontalAlignment="Right" DragDelta="ThumbBottomRightText_DragDelta_1" VerticalAlignment="Bottom"/>
                                            <Thumb x:Name="ThumbBottomLeftText" Background="Gray" Height="10" Width="10" HorizontalAlignment="Left" DragDelta="ThumbBottomLeftText_DragDelta_1" VerticalAlignment="Bottom"/>
                                            <Thumb x:Name="ThumbTopRightText" Background="Gray" Height="10" Width="10"  HorizontalAlignment="Right" DragDelta="ThumbTopRightText_DragDelta_1" VerticalAlignment="Top"/>
                                            <Thumb x:Name="ThumbTopLeftText" Background="Gray" Height="10" Width="10" HorizontalAlignment="Left" DragDelta="ThumbTopLeftText_DragDelta_1"  VerticalAlignment="Top"/>
                                            <TextBox x:Name="txtFirst"  Background="Transparent" VerticalContentAlignment="Stretch" IsSpellCheckEnabled="True" AcceptsReturn="True" IsTextPredictionEnabled="True"   TextWrapping="Wrap" Margin="5" FontSize="30"  ManipulationMode="All" FontFamily="Times New Roman"  >
                                                <TextBox.BorderBrush>
                                                    <SolidColorBrush Color="Red" />
                                                </TextBox.BorderBrush>
                                                <TextBox.RenderTransform>
                                                    <CompositeTransform />
                                                </TextBox.RenderTransform>
                                            </TextBox>
                                            <Grid.RenderTransform>
                                                <CompositeTransform />
                                            </Grid.RenderTransform>
                                        </Grid>
                                    </Canvas>
                                </Grid>
                            </ScrollViewer>
                        </Grid>
                    </Grid>
                </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>

     
    Tuesday, February 4, 2014 8:14 AM
  • Hi Zee,

    Your code will find the first TextBox with the given name, but if you want to differentiate multiple TextBoxes with the same name you'll need to build that heuristic into your algorithm. This probably isn't the right thing to do though.

    Can you please explain in more detail what you're trying to do here? What is the scenario and ultimate goal?

    I don't think that trying to enumerate the visual tree is the right solution, but I'm not sure enough of what you're doing to suggest an answer. Databinding is likely an answer

    --Rob

    Wednesday, February 5, 2014 3:05 AM
    Moderator