无法在Windows Phone应用程序中动态添加列表框内的按钮控件

时间:2012-01-05 06:20:11

标签: wpf silverlight windows-phone-7.1

我正在尝试在列表框中为每个列表框项目动态添加不同的按钮控件以及填充它时的数据。为了实现这一点,在datatemplate标签下,我添加了新的堆栈面板,其中我放置了按钮控件。现在,我正在尝试找到放置按钮的特定堆栈面板,然后根据我的条件使其显示为true或false。 所以,我试图找到所需的堆栈面板,通过循环列表框项来控制它。但是,在迭代列表框项时,我得到“空引用异常”。 以下是我的xaml代码,后面是我的xaml.cs代码:

               <ListBox x:Name="TripList" Height="465" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456" Background="White" Foreground="Blue">
                    <ListBox.ItemTemplate>
                        <DataTemplate>

                            <Border BorderBrush="Black" BorderThickness="0,0,0,4">
                                <StackPanel Orientation="Vertical" Width="456">
                                    <StackPanel Orientation="Vertical">
                                        <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                            <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" Width="370" FontSize="24" Text="{Binding PUDetails}"/>
                                            <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="24" Text="{Binding TripStatus}"/>
                                        </StackPanel>
                                        <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal" Width="300">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Conf: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding ConfNumber}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Est Do Tm: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding DOTime}"/>
                                            </StackPanel>
                                        </StackPanel>
                                        <StackPanel VerticalAlignment="Top" Orientation="Vertical">
                                            <StackPanel x:Name="stacktest" Background="Azure" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Svc: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding TripService}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="PU: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding PURoute}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="DO: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding DORoute}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Pax: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding PaxDetails}"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </StackPanel>
                                       <StackPanel x:Name="stknotchecked" VerticalAlignment="Top" Orientation="Vertical">

                                            <StackPanel Orientation="Horizontal">
                                                <Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="300" x:Name="btnaccepttrip" Content="accept trip"></Button>
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnrejecttrip" Content="reject"></Button>
                                                <Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnshowmap" Content="show map"></Button>
                                            </StackPanel>
                                        </StackPanel>
                                       <StackPanel x:Name="stkaccepted">
                                            <StackPanel Orientation="Horizontal">
                                                <Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndirections" Content="directions"></Button>
                                                <Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnflightinfo" Content="flight info"></Button>
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndetails" Content="details"></Button>
                                                <Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnlogtimes" Content="log times"></Button>
                                            </StackPanel>
                                            <StackPanel>
                                                <Button x:Name="btnstatus" Content="set status"></Button>
                                            </StackPanel>
                                        </StackPanel>
                                 </StackPanel>
                            </Border>

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>



    private void addbtncontrols()
    {

        foreach (TripsList lst in TripList.Items)
        {
            ListBoxItem item = TripList.ItemContainerGenerator.ContainerFromItem(lst) as ListBoxItem;
            Button stk = FindFirstElementInVisualTree<Button>(item);
            var stkitem =(Button)stk.FindName("btndirections");
            stkitem.Visibility = Visibility.Collapsed;   
        }
     }

    private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
    {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0)
            return null;

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(parentElement, i);

            if (child != null && child is T)
            {
                return (T)child;
            }
            else
            {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;

            }
        }
        return null;
      }

有人可以让我知道如何重新解决我的问题。提前谢谢..

2 个答案:

答案 0 :(得分:0)

导航可视树以找到这些元素并手动设置其可见性是解决此问题的错误方法!我看到你正在使用数据绑定。为什么不在您绑定到UI的模型或视图模型中公开Visibility类型的各种属性,然后可以将这些属性绑定到各种UI元素的Visibility属性以显示/隐藏它们。

顺便说一下,了解Styles,你的XAML有许多重复的样式属性,这使得它很长,很难遵循。您应该能够定义应用于所有TextBlock的TextBlock样式,以删除问题中的大部分重复代码。

答案 1 :(得分:0)

从上面的描述中,您能否提供更多相关信息:

  1. 你在哪里调用addbtncontrol方法?如果您要回复某个事件,请说:再添加一个项目,然后应该在ViewModel中完成。
  2. “你的病情”:什么样的病情?我们可以在列表框项ViewModel?
  3. 中对其进行建模

    我实现了相同的功能:包含动态项和自定义模板的列表框。还提供添加新项目的功能。所以,如果你给我更多关于你想要完成什么或更好的视图模型的信息,我可以提供帮助。