I have a listbox, which is defined like so:
<ListBox ItemsSource="{Binding Source={x:Static local:ResourceCollection.resourceList}}" Height="143" HorizontalAlignment="Left" Margin="6,6,0,0" Name="assignmentLB" VerticalAlignment="Top" Width="287" FontSize="12" FontWeight="Normal" IsEnabled="True" Grid.Column="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox />
<TextBlock Text="{Binding Content}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How can I loop through this listbox and retrieve the TextBlock.Text value for only items whose Checkbox has been checked?
Also… how can I horizontally space out the checkbox from the textblock. Right now they are right next to each other with no spacing.
Add a boolean property to the class you are binding to (the same one with the
Contentproperty) and bind theCheckBoxto it, like this:<CheckBox IsChecked="{Binding IsSelected}"/>Then you can simply loop through the
resourceListand grab all the items that haveIsSelectedset to true, like this:resourceList.Where(r => r.IsSelected);As for the horizontal spacing, you just need to supply a
Marginto either theCheckBoxor theTextBlock, or both. A margin of 5,0 on theTextBlockshould be all you need.