默认情况下会检查一些列表框项目,其中一些是未取消选中的。 然后用户可以更改已选中或未选中的项目。请告诉我如何维护已检查的ID,未经检查的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;
}
请告诉我如何维护已检查的用户ID,未经检查的用户ID .....
答案 0 :(得分:1)
假设您在代码中的某处隐藏了您的集合,而不是直接将其分配给ListBox ItemsSource属性,您应该能够只查看集合项的属性......
var checkedItems = from item in interestrates where item.IsChecked select item;
var uncheckedItems = from item in interestrates where !item.IsChecked select item;
如果只需要userIds,则将查询更改为
var checkedItems = from item in interestrates where item.IsChecked select item.Id;
var uncheckedItems = from item in interestrates where !item.IsChecked select item.Id;