This project has moved. For the latest updates, please go here.

SelectedItemsOverride is not binding correctly at all

Apr 13, 2015 at 11:22 AM
Edited Apr 13, 2015 at 11:22 AM
Hi guys,

I am using this control on a form in my project but I have a couple of issues. Basically I think it is the same problem. My Xaml looks as follows:
       <xceed:CheckComboBox ItemsSource="{Binding AllProducts}"
                                      SelectedItemsOverride="{Binding ProdChildren, Mode=TwoWay}"
                                      Grid.Row="1" Grid.Column="0"
                                      DisplayMemberPath="Title"
                                      ValueMemberPath="Id" IsTextSearchEnabled="True"/>
As you can imagine AllProducts is an observable collection of a Products Model. The ProdChildren is an observable collection of the same type that is only those that should be selected on this particular form. an example, say that AllProducts has Apple, Banana, Orange. However this record only has ProdChildren of Banana. What I would expect is that the Banana would be checked on the load of the form and that when I save the form it will interogate and find that the ProdChildren corresponds to what is selected in the drop down list at that point.

Neither of these things happen. When I load the form, the text in the combobox will be Banana but the ComboList will not have Banana selected, which means that I could select Banana in the list and have it twice in the textbox. However this also means that I cannot remove the original Banana.

Secondly, when I come to save the form, the setter for the observable collection ProdChildren is never being hit. This means that I cannot save this information to the database as the model is never being updated either. I have got around the second issue with the following code in the code behind for the view:
        ObservableCollection<ProductsSummaryModel> res = vModel.ProdSimilar;
        if (aE.IsSelected)
        {
            res.Add((ProductsSummaryModel)aE.Item);
        }
        else
        {
            res.Remove((ProductsSummaryModel)aE.Item);
        }
        vModel.ProdSimilar = res;
However, this is a massive waste of time and the original issue still stands and cannot be released in to production code as it is not feature complete.

Please let me know what I can do to get around this obvious limitation please.

Daniel
Developer
Apr 13, 2015 at 12:56 PM
Hi,

Please make sure the content of the "ProdChildren" property is an instance from the "AllProducts" property. If not, it will create a new item. Here's a working sample :
<xctk:CheckComboBox ItemsSource="{Binding AllProducts}"
                          SelectedItemsOverride="{Binding ProdChildren, Mode=TwoWay}"
                          DisplayMemberPath="Title"
                          IsTextSearchEnabled="True" 
                          Width="150"
                          Height="22"/>

public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      this.DataContext = this;
      this.AllProducts = new ObservableCollection<Product>()
      {
        new Product() { Title = "Apple", Price = 0.30},
        new Product() { Title = "Banana", Price = 0.15},
        new Product() { Title = "Orange", Price = 0.99},
      };

      this.ProdChildren = new ObservableCollection<Product>()
      {
        //this is wrong, it will create a new item that is NOT in the AllProducts collection
        //new Product() { Title = "Banana", Price = 0.15}

        //this is correct, it will add an item that IS in the AllProducts collection
        this.AllProducts[1]
      };
    }

    public ObservableCollection<Product> AllProducts
    {
      get;
      set;
    }

    public ObservableCollection<Product> ProdChildren
    {
      get;
      set;
    }
  }

  public class Product
  {
    public string Title
    {
      get;
      set;
    }

    public double Price
    {
      get;
      set;
    }
  }
For the saving part, I'm not sure I understand your Collection copy. Maybe fixing the first issue(ProdChildren being the selection in AllProducts) will fix this one.
I believe saving would be to save the selectedItems, so getting the CheckComboBox.SelectedItems would do the job ?