如何在 windows phone 中获取列表框的所有选定文本块值

how to get all selected textblock values of listbox in windows phone

我正在使用 listboxselection mode=multiple 选择多个列表框条目,而不是在按钮单击事件上我 trying/want 使用 for-each 循环获取 txtID 文本值并显示其中打印有 id 的消息框每个选定的项目。 我的列表框语法如下

<ListBox  Grid.Row="1" Name="lstAnnouncement" SelectionMode="Multiple" Width="476" d:LayoutOverrides="VerticalMargin">

        <ListBox.ItemTemplate >

            <DataTemplate>

                <StackPanel   Name="thispanel"  Grid.Row="1" Orientation="Horizontal" Height="120" Width="478" >

                    <StackPanel.Background>
                        <ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
                        <!--<SolidColorBrush  Color="{Binding Path=background}"/>-->
                    </StackPanel.Background>

                    <Grid  HorizontalAlignment="Left" Width="30" Margin="0,0,0,2" Background="#FF0195D5" Height="118">
                        <!--<Grid.Background>
                            <ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
                        </Grid.Background>-->

                    </Grid>
                    <Grid HorizontalAlignment="Left" Width="5" Height="120"/>
                    <StackPanel   Orientation="Vertical" VerticalAlignment="Top" Width="432" Height="114">

                        <StackPanel Orientation="Horizontal" Width="432" Height="27">

                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Width="433" Height="60">
                        <TextBlock x:Name="txtID" Height="56" Text="{Binding Path=announcementID}"  TextWrapping="Wrap" Foreground="Black" FontSize="18.667" Width="8" Visibility="Collapsed"/>

                        </StackPanel>
                    </StackPanel>
                </StackPanel>

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

点击事件的代码隐藏如下

  foreach (var listBoxItem in lstAnnouncement.SelectedItems)
        {
          messegebox.show(txtId.text);
          // but this(txtID) is not accessible as it is in datatemplete  
          //so how to achieve the same 

        }

您已经将 Text 绑定到 announcementID,因此您应该能够从 listBoxItem 获取它。您可以 Cast 它到您 collection 的 Type 并最终访问 collectionItem.announcementID 属性.

代码大致如下

 foreach (var listBoxItem in lstAnnouncement.SelectedItems)
        {
          var collectionItem = listBoxItem as [YourType];
          collectionItem.announcementId; //Your required text

        }

我找到了解决方法

 foreach (AnnouncementData andata in lstAnnouncement.SelectedItems)
        {
            string aa = andata.announcementID.ToString();
            MessageBox.Show(aa);
        }