当我点击列表框项目中的按钮时,如何获得列表框的完整集合?

时间:2012-12-10 20:07:50

标签: c# wpf windows-phone-7

我有一个在列表框中显示的集合。它是主要listBox的listBoxItem。每个列表框的项目(不是主要项目)都有一个文本框和按钮。所以,当我点击列表框中的任何按钮时,我需要获得所有收藏。 这是windows pnone 7应用程序。我想获得listbBox中的项目列表。 如果它不是一个集合(只是主要列表框的项目),我知道如何获得一个项目

        private void Audio_Button_Click(object sender, RoutedEventArgs e)
    {
        string uri = null;
        TextBox tb = null;
        var grid = (Grid)((Button)sender).Parent;
        foreach (var child in grid.Children)
        {
            if (child is TextBox && (string)((TextBox)child).Tag == "URL")
            {
                tb = (TextBox)child;
            }
        }
        uri = tb.Text;

但我不知道如何获得所有物品。问题在于“发送者”具有类型按钮,而不是listBoxItem。无论如何,如果它有类型listBoxItem - 我不确定我是否理解如何制作项目列表(

或者也许我可以使用另一个控件,滚动?收藏不是很大。但我想我会遇到同样的问题(

更新

                                <local:NewsTemplateSelector.Audio>
                                <DataTemplate>
                                    <Grid Name="AudioGrid" MaxHeight="2000">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="75"/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="75"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="28"/>
                                        </Grid.RowDefinitions>
                                        <Image Source="{Binding SourceImage}" Width="75"/>
                                        <TextBlock Text="{Binding SourceName}" Foreground="Black" FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
                                        <TextBlock Text="{Binding Texts}"  Foreground="Black" FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap"  VerticalAlignment="Top" Height="auto" />
                                        <Image Source="{Binding Photo[0].Big}" Margin="10" Grid.Column="1" Grid.Row="2"/>
                                        <TextBlock Text="{Binding Audio[0].Title}"  Foreground="Black" FontSize="25" Margin="96,10,10,10" Grid.Column="1" Grid.Row="3" />
                                        <Button Background="Black"  Content="Play" Grid.Column="1" HorizontalAlignment="Left" Margin="10,7,0,10" Grid.Row="3" d:LayoutOverrides="Height" Width="74" Tap="Audio_Button_Click" />
                                        <Image Source="images\like.jpg" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
                                        <TextBlock Text="{Binding Likes}" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4" Foreground="Black" TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
                                        <Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1"  />
                                        <Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
                                        <TextBox Text="{Binding Audio[0].Url}" Tag="URL" Visibility="Collapsed" />
                                    </Grid>
                                </DataTemplate>
                            </local:NewsTemplateSelector.Audio>

  <local:NewsTemplateSelector.Audios>
                                <DataTemplate>
                                    <Grid Name="AudiosGrid" MaxHeight="2000">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="75"/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="75"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="28"/>
                                        </Grid.RowDefinitions>
                                        <Image Source="{Binding SourceImage}" Width="75"/>
                                        <TextBlock Text="{Binding SourceName}" Foreground="Black" FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
                                        <TextBlock Text="{Binding Texts}"  Foreground="Black" FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap"  VerticalAlignment="Top" Height="auto" />
                                        <Image Source="{Binding Photo[0].Big}" Margin="10" Grid.Column="1" Grid.Row="2"/>
                                        <ListBox Name="audiosListbox" ItemsSource="{Binding Audio}"  MaxHeight="500" Margin="10" Grid.Column="1" Grid.Row="3" >
                                            <ListBox.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="100"/>
                                                            <ColumnDefinition/>
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock Text="{Binding Title}" Grid.Column="1" Foreground="Black" FontSize="25"  />
                                                        <Button Content="Play" Background="Black" Tap="Audios_Button_Click" />
                                                        <TextBox Text="{Binding Audio.Url}" Tag="URL" Visibility="Collapsed" />
                                                    </Grid>
                                                </DataTemplate>
                                            </ListBox.ItemTemplate>
                                        </ListBox>

                                        <Image Source="images\like.jpg" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
                                        <TextBlock Text="{Binding Likes}" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4" Foreground="Black" TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
                                        <Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1"  />
                                        <Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
                                    </Grid>
                                </DataTemplate>
                            </local:NewsTemplateSelector.Audios>

我有Collection

Simple

1 个答案:

答案 0 :(得分:0)

您可以使用Linq To Visual Treedownload code,只需一个文件)来获取包含所有项目的列表框。点击处理程序上的此代码应该适用于它。

var parentLb = ((Button)sender).Ancestors().FirstOrDefault(x => x is Listbox) as Listbox;
if(parentLb != null)
{
    // Cast the ItemsSource property to the list type you're using...
    var list = parentLb.ItemsSource
    // And do whatever you want with that list.
}

您当然应该将list投射到其原始类型。