I hope I'm going about this the right way, but if I am not, then please advise.
I start off with a loop through a strongly typed collection:
foreach (var item in myItems)
{
//magic happens here
}
Inside the foreach loop, I'm adding items to a wrapPanel, based on the collection. As part of this, each item has a checkbox. Also each item in the collection has a unique id.
What I want is when the checkbox is clicked, it adds or removes an item from a List. But even ideally would be nice to have an isSelected property in an existing List.
So I was thinking of attaching an onclick event handler to the checkbox, and somehow attaching the id (hidden from UI) to the checkbox. then inside the onclick - handle things manually.
Is there a better way to do this?
EDIT: My existing code
foreach (var x in y)
{
Grid lbItem = new Grid();
StackPanel sp = new StackPanel();
sp.Width = 190;
Border border = new Border();
border.Padding = new Thickness(10);
border.BorderBrush = new SolidColorBrush(Colors.Gainsboro);
border.BorderThickness = new Thickness(2);
border.CornerRadius = new CornerRadius(15);
CheckBox cb = new CheckBox();
cb.Content = x.id;
cb.Padding = new Thickness(0,0,5,5);
StackPanel sptitle = new StackPanel();
sptitle.Orientation = Orientation.Horizontal;
sptitle.Children.Add(cb);
TextBlock tbTitle = new TextBlock();
tbTitle.Text = x.title;
tbTitle.TextAlignment = TextAlignment.Justify;
sptitle.Children.Add(tbTitle);
TextBlock tbShortDesc = new TextBlock();
tbShortDesc.FontSize = 8;
tbShortDesc.Text = x.shortDesc;
Image imgDeal = new Image();
imgDeal.Height = 180;
imgDeal.Width = 180;
imgDeal.Source = new BitmapImage(new Uri(@"http://myurl/images/" + x.thumburl));
sp.Children.Add(sptitle);
sp.Children.Add(imgDeal);
sp.Children.Add(tbShortDesc);
border.Child = sp;
lbItem.Children.Add(border);
DealsWrapPanel.Children.Add(lbItem);
Rectangle rect = new Rectangle();
rect.Width = 5;
DealsWrapPanel.Children.Add(rect);
}