heyNow
heyNow

Reputation: 886

wpf map checkboxes to list items

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)

Upvotes: 2

Views: 609

Answers (1)

Charlie
Charlie

Reputation: 15247

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);

Upvotes: 1

Related Questions