I have:
public class Extra
{
public string Name {get;set;}
}
and I have
public class CarViewModel
{
public ObservableCollection<Extra> OwningExtras { get; set; }
public static IEnumerable<Extra> AllExtras
{
get
{
return Extra.GetAllExtras();
}
}
public CarViewModel()
{
owningExtras=Extra.GetAvailableExtrasForCar(idCar)
}
}
and CarView looks like:
<Grid >
<ListBox ItemsSource="{Binding}" Name="lbExtras">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox"/> //How implement adding and removing Extras
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
And I wonder what connect to this.DataContext? And then how to notice that something is changed (Checked checkbox means that this car has this extra)
Below code will display all items in the collection AllExtras in the listbox and fill collection CheckedExtras with items that are checked. This is only one of many ways to solve this. Not sure what you meant with “how to notice that something is changed”. Do you need to get notified that collection with checked items is updated? In that case you can use an ObservableCollection instead and listen its CollectionChanged event.
XAML:
Code behind: