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

How to use SelectedItems override

Jun 3, 2014 at 6:35 PM
Edited Jun 3, 2014 at 6:35 PM
 <xctk:CheckComboBox ItemsSource="{Binding DataItem.DependentBatches, Mode=TwoWay}"
                                                DisplayMemberPath="BatchName" 
                                                ValueMemberPath="IndexOrchestratedBatchId" 
                                                SelectedItemsOverride="{Binding DataItem.SelectedDependentBatches}"/>
My problem is the SelectedDependentBatches setter doesnt get called when the list changes.

following is the property i am binding to
private List<IndexOrchestratedBatch> _selectedDependentBatches;
        public List<IndexOrchestratedBatch> SelectedDependentBatches
        {
            get
            {
                return _selectedDependentBatches;
            }
            set
            {
                _selectedDependentBatches = value;
            }
        }
Developer
Jun 4, 2014 at 1:04 PM
Hi,

Can you use an ObservableCollection instead of a List ? Or a DP to see the list change in a callback ?
I've made a sample inspire from yours. I use a DP to be notified when the Collection is modified and an event to be notified when the content of the collection is changed.

Hope this helps :
 <xctk:CheckComboBox x:Name="_checkComboBox"
                          ItemsSource="{Binding DataItem.DependentBatches, Mode=TwoWay}"
                          DisplayMemberPath="BatchName"
                          ValueMemberPath="IndexOrchestratedBatchId"
                          SelectedItemsOverride="{Binding DataItem.SelectedDependentBatches}" />

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

      _checkComboBox.DataContext = this;

      this.DataItem = new MyDataItem();
      this.DataItem.DependentBatches = new ObservableCollection<MySubDataItem>()
      {
        new MySubDataItem() { BatchName = "First", IndexOrchestratedBatchId = 1 },
        new MySubDataItem() { BatchName = "Second", IndexOrchestratedBatchId = 2 },
        new MySubDataItem() { BatchName = "Third", IndexOrchestratedBatchId = 3 },
        new MySubDataItem() { BatchName = "Fourth", IndexOrchestratedBatchId = 4 },
        new MySubDataItem() { BatchName = "Fifth", IndexOrchestratedBatchId = 5 },
      };

      this.DataItem.SelectedDependentBatches = new ObservableCollection<MySubDataItem>()
      {
        this.DataItem.DependentBatches[2] //initial selected item
      };

      this.DataItem.SelectedDependentBatches.CollectionChanged += this.SelectedDependentBatches_CollectionChanged;

    }

    private void SelectedDependentBatches_CollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
    {
      System.Diagnostics.Debug.WriteLine("Items in collection changed");
    }

    public MyDataItem DataItem
    {
      get;
      set;
    }
  }

  public class MyDataItem : DependencyObject
  {
    public ObservableCollection<MySubDataItem> DependentBatches
    {
      get;
      set;
    }

    #region SelectedDependentBatches

    public static readonly DependencyProperty SelectedDependentBatchesProperty = 
      DependencyProperty.Register( "SelectedDependentBatches", typeof( ObservableCollection<MySubDataItem> ), typeof( MyDataItem )
                                   , new FrameworkPropertyMetadata( null, MyDataItem.OnSelectedDependentBatchesChanged ) );

    public ObservableCollection<MySubDataItem> SelectedDependentBatches
    {
      get
      {
        return ( ObservableCollection<MySubDataItem> )this.GetValue( MyDataItem.SelectedDependentBatchesProperty );
      }
      set
      {
        this.SetValue( MyDataItem.SelectedDependentBatchesProperty, value );
      }
    }

    private static void OnSelectedDependentBatchesChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
      ( ( MyDataItem )d ).OnSelectedDependentBatchesChanged( e );
    }

    protected virtual void OnSelectedDependentBatchesChanged( DependencyPropertyChangedEventArgs e )
    {
      System.Diagnostics.Debug.WriteLine( "Collection changed" );
    }

    #endregion
  }

  public class MySubDataItem
  {
    public string BatchName
    {
      get;
      set;
    }

    public int IndexOrchestratedBatchId
    {
      get;
      set;
    }
  }