繁体   English   中英

如何在Windows Phone 8列表中添加选中的联系人?

[英]How to add checked contact in list Windows Phone 8?

我想按我的代码在列表中添加联系人,

 <!--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);
        }
    }

我从谷歌那里得到了这种方法,看起来不错,但是问题是我没有在这一行中得到“查找名称”,

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++;  
        }

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

希望您的建议

谢谢

using Microsoft.Phone.UserData;

//使用该名称空间,请遵循以下内容//即兴一点

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

                    });
                }
                lstbxk.ItemsSource = a.cnts;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM