Question

I have db which filling my listbox and before next step, I want to have a chance to customize choice of user with tooglebuttons.

So I want to get the info about his choices in codebehind. I find in this answer the code which uses VisualTreeHelper.

This is my xaml code

 <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <controls:Pivot>
            <controls:PivotItem>
                <ListBox x:Name="PizzaList" SelectionChanged="PizzaList_SelectionChanged" IsSynchronizedWithCurrentItem="false" >
                    <ListBox.ItemTemplate>
                        <DataTemplate x:Name="template">

                            <toolkit:ExpanderView Header="{Binding Nazwa}" x:Name="expander" Style="{StaticResource ExpanderViewStyle}">
                                <toolkit:ExpanderView.Items>
                                   <!--first stack panel would contain all elements which would be showed 
                                    after clicking on listbox item-->
                                    <StackPanel Margin="20,0,0,0" Orientation="Vertical">
                                        <TextBlock HorizontalAlignment="Left" Text="Rozmiar"></TextBlock>                               
                                        <!-- this stack panel contains 2 buttons which are
                                        connected to sizes of pizza -->
                                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                            <ToggleButton Width="200">
                                               <!--this how would look the button's content-->
                                                <StackPanel HorizontalAlignment="Center" Orientation="Vertical">
                                                    <TextBlock>&#8960;28cm</TextBlock>
                                                    <TextBlock Text="{Binding Cenaa}"></TextBlock>
                                                </StackPanel>
                                            </ToggleButton>
                                            <ToggleButton Width="200">
                                                <StackPanel HorizontalAlignment="Center" Orientation="Vertical">
                                                    <TextBlock>&#8960;50cm</TextBlock>
                                                    <TextBlock Text="{Binding Cenab}"></TextBlock>
                                                </StackPanel>
                                            </ToggleButton>
                                        </StackPanel>
                                        <!-- here part of code which would show type of cake option-->
                                        <TextBlock Margin="0,12,0,0">Grubość ciasta:</TextBlock>
                                        <StackPanel Orientation="Horizontal">
                                            <ToggleButton x:Name="small" IsChecked="true">
                                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">small</TextBlock>
                                            </ToggleButton>
                                            <ToggleButton x:Name="middle" Checked="Grubosc_Checked">
                                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">middle</TextBlock>
                                            </ToggleButton>
                                            <ToggleButton x:Name="fat" Checked="Grubosc_Checked">
                                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">fat</TextBlock>
                                            </ToggleButton>
                                        </StackPanel>
                                        <Button>edit</Button>
                                        <Button>basket</Button>
                                    </StackPanel>
                                </toolkit:ExpanderView.Items>
                                <toolkit:ExpanderView.Expander>
                                    <TextBlock Text="{Binding Skladniki}" Width="500"></TextBlock>
                                </toolkit:ExpanderView.Expander>
                            </toolkit:ExpanderView>


                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </controls:PivotItem>
        </controls:Pivot>
    </Grid>

and here is the method taken from answer:

      public static T FindChild<T>(DependencyObject parent, string childName)
   where T : DependencyObject
        {
            // Confirm parent and childName are valid. 
            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);
                // If the child is not of the request child type child
                T childType = child as T;
                if (childType == null)
                {
                    // recursively drill down the tree
                    foundChild = FindChild<T>(child, childName);

                    // If the child is found, break so we do not overwrite the found child. 
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }

and I try to find my child by this line

var found = FindChild<ToggleButton>(template, "small");

and then I get the error in this line:

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

An exception of type 'System.InvalidOperationException' occurred in System.Windows.ni.dll but was not handled in user code"

What is my mistake? How should I get access to this buttons?

Was it helpful?

Solution

Well, somewhere in your FindChild method there is a bug, it's hard to tell without debugging. Try this method (I wrote it 3+ years ago and haven't had any issues):

public static T FindVisualChildByName<T> (this FrameworkElement elem, string name) where T : DependencyObject
    {
        for (var i = 0; i < VisualTreeHelper.GetChildrenCount (elem); i++)
        {
            var child = VisualTreeHelper.GetChild (elem, i) as FrameworkElement;
            if (child == null)
                continue;

            var controlName = child.GetValue (Control.NameProperty) as string;
            if (controlName == name)
                return child as T;

            var result = FindVisualChildByName<T> (child, name);
            if (result != null)
                return result;
        }
        return null;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top