tags:

views:

56

answers:

1

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)

+1  A: 

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:

<Window x:Class="ListBoxTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <StackPanel>

        <ListBox ItemsSource="{Binding Path=AllExtras}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Click="OnCheckBoxClick" /> 
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </StackPanel>

</Window>

Code behind:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace ListBoxTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataContext = new VM();
        }

        private void OnCheckBoxClick(object sender, RoutedEventArgs e)
        {
            VM vm = DataContext as VM;
            CheckBox checkBox = sender as CheckBox;
            Extra extra = checkBox.DataContext as Extra;
            if (checkBox.IsChecked.GetValueOrDefault())
            {
                vm.CheckedExtras.Add(extra);
            }
            else
            {
                vm.CheckedExtras.Remove(extra);
            }
        }
    }

    public class VM
    {
        public VM()
        {
            CheckedExtras = new List<Extra>();
            AllExtras = new List<Extra>() { new Extra() { Name = "A" }, new Extra() { Name = "B" }, new Extra() { Name = "C" }, };
        }

        public List<Extra> CheckedExtras { get; private set; }
        public List<Extra> AllExtras { get; private set; }
    }

    public class Extra
    {
        public string Name { get; set; }
    } 
}
Wallstreet Programmer
It's close, but when I change to CheckedExtras = new List<Extra>(){ new Extra() { Name = "A" }}; and run my application all checkboxes are still unchecked. Thecollection with checked items is never updated.The list of AllExtras is constant.
Would it be OK to add a bool property to Extra class if is checked? Then the checkbox can be databund to the bool property. CheckedExtras would just be a linq query filtering out Extra objects with the bool property set to true.
Wallstreet Programmer
CheckedExtras is updated , see OnCheckBoxClick method. What do you mean allExtras is constant?
Wallstreet Programmer
allExtras is constant means that I load allExtras for example from xml, and user can't change this list. He can only change list of extras related with car (by checkbox). Idea with bool field is interesting. I'll check it. Good job ! :)
I would suggest having the 'Extra' class wrapped inside a viewmodel for each item within the list. That way you can bind the IsChecked to a bool property on the viewmodel, or even a command. The underlying model would not have to change
aqwert