默認情況下,某些列表框項目被選中,其中一些列表框被取消選中。 然後用戶可以能夠改變選中或取消選中的items.please告訴我如何維護檢查IDS,IDS選中..如何獲取wp7中列表框中已選中和未選中複選框項目的ID?
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
ListBoxItem checedItem = this.listBox2.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
if (checedItem != null)
{
checedItem.IsSelected = true;
}
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
ListBoxItem checedItem = this.listBox2.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
if (checedItem != null)
{
checedItem.IsSelected = false;
}
}
string result1 = e.Result.ToString();
XDocument xmlDocu = XDocument.Load(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(result)));
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
IsChecked = !(Boolean)rts.Element("visiblestatus"),
}).ToList<SampleCheckedData>();
this.listBox2.ItemsSource = interestrates;
<ListBox x:Name="listBox2" SelectionMode="Multiple" Margin="0,67,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon}" Stretch="Fill" MaxHeight="Infinity" MaxWidth="Infinity" Width="100" Height="100" />
<TextBlock Text="{Binding Name}" Width="300" VerticalAlignment="Center" TextWrapping="NoWrap" FontSize="22" />
<CheckBox IsChecked="{Binding IsChecked}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
編輯:
private void btnDone_Click(object sender, RoutedEventArgs e)
{
var checkedItems = from item in interestrates where item.IsChecked select item.Id;
var uncheckedItems = from item in interestrates where !item.IsChecked select item.Id;
}
請告訴我如何維護檢查userids,unchecked userids .....
查詢到的東西也顯示覆選框的XAML在列表框中 – nkchandra