从列表框中获取多个选中的复选框值
本文关键字:复选框 列表 获取 | 更新日期: 2023-09-27 18:05:35
在我的windows phone应用程序中,我得到了所有的联系人和附加复选框,如下所示:
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>
和下面是示例:
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
只让我选中复选框的值,但我想从列表框中获得多个选中的复选框值,请建议我,我做什么,等待你的回复。谢谢。
您是否尝试使用SelectedItems(以's'结尾)属性?根据文档列表框有,它似乎正是你正在寻找的。
你可能需要修改ListBox的SelectionMode属性。
当SelectionMode为Multiple或Extended时,使用SelectedItems属性获取所选项。当SelectionMode为单,使用选择器。SelectedItem属性来获取选中。
同样,你应该在你的ItemTemplate中为复选框设置一个绑定,以使其工作:
<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>