从列表框中获取多个选中的复选框值

时间:2014-05-27 13:29:56

标签: c# windows-phone-8 listbox

在我的Windows手机应用程序中,我收到所有联系人并附上以下各项的复选框:

xaml page
        <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>
            <Button x:Name="btn_addContacts"
                    Content="Add"
                    Width="200"
                    Height="70"
                    FontSize="25"
                    Foreground="Blue"
                    Background="AliceBlue"
                    Click="btn_addContacts_Click" Margin="130,496,126,-89"></Button>

        </Grid>

以下是xaml.cs页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Data;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    public partial class SelectionOfContacts : PhoneApplicationPage
    {
        List<CustomContact> listOfContact = new List<CustomContact>();
        public SelectionOfContacts()
        {
            InitializeComponent();

            Contacts cons = new Contacts();
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
            cons.SearchAsync(string.Empty, FilterKind.None, "Contacts Test #1");
        }
        private void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            try
            {
                //List<CustomContact> listOfContact = new List<CustomContact>();
                foreach (var c in e.Results)
                {
                    CustomContact contact = new CustomContact();
                    contact.Name = c.DisplayName;
                    int count = c.PhoneNumbers.Count();
                    for (int i = 0; i < count; i++)
                    {
                        if (count > 0 && c.PhoneNumbers.ElementAt(i).PhoneNumber != null && !string.IsNullOrEmpty(c.PhoneNumbers.ElementAt(i).PhoneNumber))
                        {
                            contact.Number[i] = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
                        }
                        else
                        {
                            contact.Number[i] = "";
                        }
                    }
                    listOfContact.Add(contact);

                }

                ContactResultsData.ItemsSource = listOfContact;

            }
            catch (System.Exception)
            {
                //No results
            }
            if (ContactResultsData.Items.Any())
            {
                ContactResultsLabel.Text = "results";
            }
            else
            {
                ContactResultsLabel.Text = "no results";
            }
        }

        private void btn_addContacts_Click(object sender, RoutedEventArgs e)
        {
            var selectvalue = ContactResultsData.SelectedItem;
        }
    }
}

它的工作正常且在这一行var selectvalue = ContactResultsData.SelectedItem只获得了复选框的选定值,但我想从列表框中获取多个选中的复选框值,请建议我,我做什么,等待你的回复。 感谢。

1 个答案:

答案 0 :(得分:2)

您是否尝试使用SelectedItems(最后的&#39;)?根据ListBoxes的文档,它看起来正是你正在寻找的。

您可能必须修改ListBox的SelectionMode属性。

  

当SelectionMode为Multiple或Extended时,请使用SelectedItems   获取所选项目的属性。当SelectionMode是   单击,使用Selector.SelectedItem属性获取项目   地选择。

此外,您应该在ItemTemplate中为CheckBox设置一个Binding以使其正常工作:

<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>