【问题标题】:how to get all selected textblock values of listbox in windows phone如何在Windows Phone中获取列表框的所有选定文本块值
【发布时间】:2015-01-21 05:14:51
【问题描述】:

我正在使用 listboxselection mode=multiple 选择多个列表框条目,而不是在按钮单击事件上,我正在尝试/想要使用 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 

        }

【问题讨论】:

    标签: c# windows-phone-8 foreach listbox


    【解决方案1】:

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

    代码大致如下所示

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

    【讨论】:

    • 嗨,感谢您的帮助,但我使用这个 foreach 解决了(lstAnnouncement.SelectedItems 中的 AnnouncementData andata){ string aa = andata.announcementID.ToString();消息框.Show(aa); }
    【解决方案2】:

    我得到了我这样做的解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      相关资源
      最近更新 更多