2

我有填充我的列表框的数据库,在下一步之前,我希望有机会使用工具按钮自定义用户选择。

所以我想在代码隐藏中获取有关他选择的信息。我在这个答案中找到了使用 VisualTreeHelper 的代码。

这是我的 xaml 代码

 <!--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>

这是取自答案的方法:

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

我试着通过这条线找到我的孩子

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

然后我在这一行得到错误:

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

System.Windows.ni.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理”

我的错误是什么?我应该如何访问此按钮?

4

1 回答 1

0

好吧,在你的FindChild方法的某个地方有一个错误,如果没有调试就很难说出来。试试这个方法(我在 3 年前写的,没有任何问题):

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;
    }
于 2014-01-15T18:04:46.413 回答