如何在列表WindowsPhone8中添加选中的联系人

本文关键字:添加 联系人 WindowsPhone8 列表 | 更新日期: 2023-09-27 18:05:42

我正试图在列表中添加联系人,因为我的代码是

 <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,10">
                <TextBlock x:Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap"></TextBlock>
                <ListBox x:Name="ContactResultsData" ItemsSource="{Binding listOfContacts}" Height="293" Margin="24,0,0,0">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                            <CheckBox Name="contactChk" IsChecked="false" Foreground="Black" Background="Black" BorderBrush="White"></CheckBox>
                            <TextBlock x:Name="ContactResultsName" Text="{Binding Name}" FontSize="50"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>

无法在列表框下获取联系人我正在使用这种方法点击按钮,

private void GetSelectedCheckObjItem()
    {
        try
        {
            for (int i = 0; i < ContactResultsData.Items.Count; i++)
            {
                // Get a all list items from listbox
                ListBoxItem ListBoxItemObj = (ListBoxItem)ContactResultsData.ItemContainerGenerator.ContainerFromItem(ContactResultsData.Items[i]);
                // find a ContentPresenter of that list item.. [Call FindVisualChild Method]
                ContentPresenter ContentPresenterObj = FindVisualChild<ContentPresenter>(ListBoxItemObj);
                // call FindName on the DataTemplate of that ContentPresenter
                DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate;
                CheckBox Chk = (CheckBox)DataTemplateObj.FindName("contactChk", ContentPresenterObj);
                // get a selected checkbox items.
                if (Chk.IsChecked == true)
                {
                    MessageBox.Show(Chk.Content.ToString().Trim());
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

我从谷歌得到了这个方法,它看起来很好,但问题是我在这行中没有得到"Findname",

CheckBox Chk = (CheckBox)DataTemplateObj.FindName("contactChk", ContentPresenterObj);

我也试过这个,

<ListBox x:Name="ContactResultsData" SelectionMode="Multiple" ItemsSource="{Binding listOfContacts}" Height="293" Margin="24,0,0,0">
     <ListBox.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                 <CheckBox Name="contactChk" 
                           IsChecked={Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
                                         Path=IsSelected}"
                           Foreground="Black" Background="Black" BorderBrush="White">  
                 </CheckBox>
                    <TextBlock x:Name="ContactResultsName" Text="{Binding Name}" FontSize="50"></TextBlock>
               </StackPanel>
          </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

关于使用这个我没有得到这些属性,

Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}

我得到的属性是,

Self and TemplatedParent

在列表中添加联系人有其他选择吗。我在点击复选框时发出警报,

 private void contactChk_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chk2 = (CheckBox)sender;
            MessageBox.Show("" + chk2.IsChecked);
            count++;  
        }

也许我可以通过这种方法在列表中添加联系人

希望你的建议

感谢

如何在列表WindowsPhone8中添加选中的联系人

using Microsoft.Phone.UserData;

//使用此命名空间F允许下面的//即兴制作一点

  Contacts cons = new Contacts();
                       foreach(var c in con)
                    {
                        a.cnts.Add(new Contactss()
                        {
                            name=c.name,
                            number=c.phone
                    });
                }
                lstbxk.ItemsSource = a.cnts;