2

i have a grid with checkboxes and textboxes, a list list1 as list(of Stuff) fills up the textboxes. prop1 is a property of Stuff

<Grid Name="MainGrid">  
   <ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
     <DataTemplate>
       <Grid Name="g1">  
         <CheckBox Grid.Column="0" Checked="CheckBox_Checked"/>
         <TextBox Grid.Column="1" Name="TextBox1" Text="{Binding prop1}" />
       </Grid>
     </DataTemplate>
   </ItemsControl.ItemTemplate>
  </ItemsControl>
</Grid>

How can i use the checkboxes to make a new list of only the checked values?

EDIT: Fixed XAML(2)

heyNow
  • 866
  • 2
  • 19
  • 42
  • 1
    Your XAML isn't valid. You can't have a `DataTemplate` in a `Grid` like that. – CodeNaked Jul 26 '11 at 18:31
  • similar question as http://stackoverflow.com/questions/859227/looking-for-a-wpf-combobox-with-checkboxes i added another property isSelected to Stuff to handle its selected(ness) – heyNow Jul 26 '11 at 19:55

1 Answers1

1

Make a wrapper class around Stuff that adds a boolean IsChecked property. Bind the CheckBox.IsChecked property to StuffWrapper.IsChecked, and bind your TextBox.Text property to StuffWrapper.Stuff. Instead of storing your ItemsSource as List<Stuff>, it is now List<StuffWrapper>.

Then, to make a new list of only the checked items, use the Linq "Where" function as follows:

var checkedList = list1.Where(s => s.IsChecked);

Charlie
  • 15,069
  • 3
  • 64
  • 70