Sahu Vijay
Sahu Vijay

Reputation: 31

Find all controls inside WPF Listbox

I want to access all buttons inside the WPF Listbox, here is my xaml code.

<Grid>
    <ScrollViewer x:Name="myScrollViewer" HorizontalScrollBarVisibility="Disabled" MouseDown="myStackPanel_MouseDown"  MouseUp="myScrollViewer_MouseMove"  VerticalScrollBarVisibility="Hidden" Height="435" BorderBrush="{x:Null}" Width="250" Margin="0,-4,-4,-4" ScrollChanged="scrollerVideoCategory_ScrollChanged">
        <StackPanel Orientation="Vertical" Name="StackPanleEmployee" Margin="40,0,0,0" MouseDown="myStackPanel_MouseDown"  MouseUp="myScrollViewer_MouseMove"   Loaded="StackPanleVideoCategory_Loaded" >
            <ListBox x:Name="ListBoxEmployeVideoType" MouseDown="myStackPanel_MouseDown"  MouseUp="myScrollViewer_MouseMove"  BorderThickness="0,1,0,0" BorderBrush="#E2E2E2">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" Width="200" Name="myStackPanel" MouseDown="myStackPanel_MouseDown"  MouseUp="myScrollViewer_MouseMove" >
                            <Button  VerticalAlignment="Center" Style="{StaticResource ButtonVideoTabItemDefault}"   MouseUp="b_MouseMove"  Tag="{Binding Path}" Content="{Binding Name}" Name="btnSubCategory"  Click="btnVideoCategorySelection_Click"></Button>
                            <Separator Background="#E2E2E2" Margin="0"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </ScrollViewer>
</Grid>

anyone have idea?

Upvotes: 1

Views: 2616

Answers (2)

sa_ddam213
sa_ddam213

Reputation: 43636

Somthing like this may do the trick

        /// <summary>
        /// Finds the visual child.
        /// </summary>
        /// <typeparam name="childItem">The type of the child item.</typeparam>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        private IEnumerable<T> FindVisualChildren<T>(DependencyObject obj) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }
                else
                {
                    var childOfChild = FindVisualChildren<T>(child);
                    if (childOfChild != null)
                    {
                        foreach (var subchild in childOfChild)
                        {
                            yield return subchild;
                        }
                    }
                }
            }
        }

Just declare the type you want and the control that contains them

  var buttons = FindVisualChildren<Button>(ListBoxEmployeVideoType).ToList();

This is just a quick example and can be expanded to your needs.

Upvotes: 3

Adil
Adil

Reputation: 3268

You can use GetChildren() method of VisualTreeHelper or LogicalTreeHelper class. Ref SDK

Visual tree represents all elements that renders in UI whereas logical tree essential structure of UI. You can read about details about the usage here

Hope this helps.

Upvotes: 0

Related Questions