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

CheckComboBox enqueries

Jan 17, 2014 at 7:12 AM
Edited Jan 17, 2014 at 11:16 AM
I have 2 enqueries:

1st: How do I enable say first or second item (or first and second item) of the CheckComboBox to be checked upon Windows_Loaded?

2nd: Say I use the CheckComboBox to update a CheckListBox as below, in a ItemSelectionChanged event:

private void CheckComboBox_CostCenter_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
    {
        CheckListBox_TestSystems.Items.Clear();
        int SelectedItemsTotal = CheckComboBox_CostCenter.SelectedItems.Count;
        string[] selectedCostCenter = new string[SelectedItemsTotal];

        for (int j = 0; j < SelectedItemsTotal; j++)
        {
            selectedCostCenter[j] = CheckComboBox_CostCenter.SelectedItems[j].ToString();
            FileInfo file = new FileInfo(string.Concat(path1, selectedCostCenter[j], ".txt"));
            StreamReader stRead = file.OpenText();
            while (!stRead.EndOfStream)
            {
                CheckListBox_TestSystems.Items.Add(stRead.ReadLine());
            }
            stRead.Close();
        }            
    }
When user says check Item1 from CheckComboBox_CostCenter, a CheckList_TestSystems would be populated with its items (say ItemA, ItemB, ItemC).
When user says check Item2 from CheckComboBox_CostCenter (with item1 unchecked), a CheckList_TestSystems would be populated with its items (say ItemD, ItemE, ItemF).
When user says check both Item1 and Item2 from CheckComboBox_CostCenter, a CheckList_TestSystems would be populated with its items (say ItemA, ItemB, ItemC, ItemD, ItemE, ItemF).
Above works perfectly fine.

My main issue is this:
When user check Item1 from CheckComboBox_CostCenter, and ItemA, ItemB, ItemC will appear on its CheckList_TestSystems. Say now, I want to check ItemA (on the CheckList_TestSystems). Next, I go to check Item2 from the CheckComboBox_CostCenter, with Item1 still remain checked. Now the CheckList_TestSystems is populated with ItemA, ItemB, ItemC, ItemD, ItemE, ItemF. But the previously checked of the ItemA will become UNCHECKED. I would like the checked ItemA to remain as it is when user click the Item2.

I understand that in the first line of the ItemSelectionChanged event, there is CheckListBox_TestSystems.Items.Clear(); which probably clear the checked ItemA. But there is necessary also. If it is not there, when user first check Item1, ItemA, ItemB, ItemC would be populated in CheckList_TestSystems. When user uncheck Item1, then ItemA, ItemB, ItemC would not be clear. If check Item1 again, then there would be ItemA, ItemB, ItemC, ItemA, ItemB, ItemC, which is incorrect.

How do I overcome this scenario?
thanks.

thanks.
Developer
Jan 17, 2014 at 6:00 PM
Hi,

1) to start the CheckComboBox with selectedItems, you can set the SelectedItemsOverride property :
 List<string> myList = new List<string>() { "abc", "def", "ghi", "jkl" };
      _checkComboBox.ItemsSource = myList;
      _checkComboBox.SelectedItemsOverride = new List<string>() { myList[ 0 ], myList[1] };
or set the SelectecValue property ;
 List<string> myList = new List<string>() { "abc", "def", "ghi", "jkl" };
      _checkComboBox.ItemsSource = myList;
      _checkComboBox.SelectedValue = "abc,def";
2) The check is removed in the CheckListBox because you clear the CheckList and fill it with new items. If you find a way to re-use the same object for filling the CheckListBox, it should work, Here's an example :
 public partial class MainWindow : Window
  {
    List<DataObject> DataObjectList = new List<DataObject>()
    {
      new DataObject( "A" ),
      new DataObject( "B" ),
      new DataObject( "C" ),
      new DataObject( "D" ),
      new DataObject( "E" ),
      new DataObject( "F" ),
      new DataObject( "G" ),
      new DataObject( "H" ),
      new DataObject( "I" ),
      new DataObject( "J" ),
      new DataObject( "K" ),
      new DataObject( "L" )
    };

    public MainWindow()
    {
      InitializeComponent();

      List<string> myList = new List<string>() { "abc", "def", "ghi", "jkl" };
      _checkComboBox.ItemsSource = myList;
    }

    private void CheckComboBox_CostCenter_ItemSelectionChanged( object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e )
    {
      _checkListBox.Items.Clear();
      int SelectedItemsTotal = _checkComboBox.SelectedItems.Count;
      string[] selectedCostCenter = new string[ SelectedItemsTotal ];

      for( int j = 0; j < SelectedItemsTotal; j++ )
      {
        selectedCostCenter[ j ] = _checkComboBox.SelectedItems[ j ].ToString();
        if( selectedCostCenter[ j ] == "abc" )
        {
          _checkListBox.Items.Add( DataObjectList[ 0 ] );
          _checkListBox.Items.Add( DataObjectList[ 1 ] );
          _checkListBox.Items.Add( DataObjectList[ 2 ] );
        }
        else if( selectedCostCenter[ j ] == "def" )
        {
          _checkListBox.Items.Add( DataObjectList[ 3 ] );
          _checkListBox.Items.Add( DataObjectList[ 4 ] );
          _checkListBox.Items.Add( DataObjectList[ 5 ] );
        }
        else if( selectedCostCenter[ j ] == "ghi" )
        {
          _checkListBox.Items.Add( DataObjectList[ 6 ] );
          _checkListBox.Items.Add( DataObjectList[ 7 ] );
          _checkListBox.Items.Add( DataObjectList[ 8 ] );
        }
        else if( selectedCostCenter[ j ] == "jkl" )
        {
          _checkListBox.Items.Add( DataObjectList[ 9 ] );
          _checkListBox.Items.Add( DataObjectList[ 10 ] );
          _checkListBox.Items.Add( DataObjectList[ 11 ] );
        }
      }
    }
  }

  public class DataObject
  {
    public DataObject( string data )
    {
      this.Data = data;
    }

    public string Data
    {
      get;
      set;
    }

    public override string ToString()
    {
      return this.Data;
    }
  }
Here's another way of doing the CheckComboBox_CostCenter_ItemSelectionChanged method without clearing the CheckListBox:
 private void CheckComboBox_CostCenter_ItemSelectionChanged( object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e )
    {
      if( e.IsSelected )
      {
        if( e.Item.ToString() == "abc" )
        {
          _checkListBox.Items.Add( DataObjectList[ 0 ] );
          _checkListBox.Items.Add( DataObjectList[ 1 ] );
          _checkListBox.Items.Add( DataObjectList[ 2 ] );
        }
        else if( e.Item.ToString() == "def" )
        {
          _checkListBox.Items.Add( DataObjectList[ 3 ] );
          _checkListBox.Items.Add( DataObjectList[ 4 ] );
          _checkListBox.Items.Add( DataObjectList[ 5 ] );
        }
        else if( e.Item.ToString() == "ghi" )
        {
          _checkListBox.Items.Add( DataObjectList[ 6 ] );
          _checkListBox.Items.Add( DataObjectList[ 7 ] );
          _checkListBox.Items.Add( DataObjectList[ 8 ] );
        }
        else if( e.Item.ToString() == "jkl" )
        {
          _checkListBox.Items.Add( DataObjectList[ 9 ] );
          _checkListBox.Items.Add( DataObjectList[ 10 ] );
          _checkListBox.Items.Add( DataObjectList[ 11 ] );
        }
      }
      else
      {
        if( e.Item.ToString() == "abc" )
        {
          _checkListBox.Items.Remove( DataObjectList[ 0 ] );
          _checkListBox.Items.Remove( DataObjectList[ 1 ] );
          _checkListBox.Items.Remove( DataObjectList[ 2 ] );
        }
        else if( e.Item.ToString() == "def" )
        {
          _checkListBox.Items.Remove( DataObjectList[ 3 ] );
          _checkListBox.Items.Remove( DataObjectList[ 4 ] );
          _checkListBox.Items.Remove( DataObjectList[ 5 ] );
        }
        else if( e.Item.ToString() == "ghi" )
        {
          _checkListBox.Items.Remove( DataObjectList[ 6 ] );
          _checkListBox.Items.Remove( DataObjectList[ 7 ] );
          _checkListBox.Items.Remove( DataObjectList[ 8 ] );
        }
        else if( e.Item.ToString() == "jkl" )
        {
          _checkListBox.Items.Remove( DataObjectList[ 9 ] );
          _checkListBox.Items.Remove( DataObjectList[ 10 ] );
          _checkListBox.Items.Remove( DataObjectList[ 11 ] );
        }
      }
    }
Jan 20, 2014 at 6:16 AM
Hi BoucherS,

the first answer to (1) does not work. I have this and I do not see any of the first and second items being checked upon Windows_Loaded.

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //string[] CostCenterName = { "Analog", "Digital", "EMS", "INT1", "INT2", "INT3", "Photo", "QA", "SMT1", "SMT2", "US-PF", "US-PXS" };
        //CheckComboBox_CostCenter.ItemsSource = CostCenterName.ToList();
        //CheckComboBox_CostCenter.SelectedItemsOverride = new List<string>() { CostCenterName[0], CostCenterName[1] };
        List<string> myList = new List<string>() { "abc", "def", "ghi", "jkl" };
        CheckComboBox_CostCenter.ItemsSource = myList;
        CheckComboBox_CostCenter.SelectedItemsOverride = new List<string>() { myList[0], myList[1] };
        CheckComboBox_CostCenter.SelectedValue = "abc,def";
    }
For Part (2), the second part of your code works for your case.
but when I try to implement it for my case, such that it loads from a text file, it does not work. Say when I checked a item on CheckListBox_TestSystems, next choose another item to check on the CheckComboBox_CostCenter, it does add to the CheckListBox_TestSystems, with the initial checked item on CheckListBox_TestSystems still remain as checked. This is what I want.
But when I uncheck previously checked items on the CheckComboBox_CostCenter, those items which supposed to be removed from the CheckListBox_TestSystems still remain there. That means this part of the code ( CheckListBox_TestSystems.Items.Remove(stRead.ReadLine());) does not work.
   private void CheckComboBox_CostCenter_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
    {
        int SelectedItemsTotal = CheckComboBox_CostCenter.SelectedItems.Count;
        string[] selectedCostCenter = new string[SelectedItemsTotal];

        for (int j = 0; j < SelectedItemsTotal; j++)
        {
            selectedCostCenter[j] = CheckComboBox_CostCenter.SelectedItems[j].ToString();
            if (e.IsSelected)
            {
                if (e.Item.ToString() == selectedCostCenter[j])
                {
                    FileInfo file = new FileInfo(string.Concat(path1, selectedCostCenter[j], ".txt"));
                    StreamReader stRead = file.OpenText();
                    while (!stRead.EndOfStream)
                    {
                        CheckListBox_TestSystems.Items.Add(stRead.ReadLine());
                    }
                    stRead.Close();
                }
            }
            else
            {
                if (e.Item.ToString() == selectedCostCenter[j])
                {
                    FileInfo file = new FileInfo(string.Concat(path1, selectedCostCenter[j], ".txt"));
                    StreamReader stRead = file.OpenText();
                    while (!stRead.EndOfStream)
                    {
                        CheckListBox_TestSystems.Items.Remove(stRead.ReadLine());
                    }
                    stRead.Close();
                }
            }
        }
    }
Developer
Jan 21, 2014 at 2:07 PM
Hi,

For Part 1), I tried your code (without the commented lines) and it worked fine : the CheckComboBox starts with item selected. Using "SelectedItemsOverride" and/or "SelectedValue " will check the 2 first entries in the CheckComboBox, which are "abc" and "def". I tried activating the selection it in the constructor of the Window and/or in the Loaded callback of the Window and it still worked. I tried this selection in Toolkit v1.9 and v2.0 with success. It has to be something else, maybe in xaml ?

For Part 2) I reproduce the bug. You are doing a For loop on SelectedItemsTotal. When adding, it will work. When you select an item from CheckComboBox, SelectedItemsTotal will go from 0 to 1 and the read and addition will be done to CheckListBox. When selecting a 2nd item from CheckComboBox, SelectedItemsTotal will go from 1 to 2 and the read and addition will be done on the 2nd loop to CheckListBox.

But when removing an item from CheckComboBox, if you have 2 selectedItems ("abc" and "def") and you remove "def", CheckComboBox.SelectedItems will contain "abc". But your "selectedCostCenter[ j ]" will contain "abc" while "e.Item" will contain "def" (the removedItem). Since it's not equal, no removing will be done in CheckListBox.

You should find another way of detecting the removedItem. I confirm that the line :
 _checkListBox.Items.Remove( stRead.ReadLine() );
works to remove data from CheckListBox.
Jan 23, 2014 at 1:46 AM
For Part (1), I realize that I need to remove SelectedValue="{Binding SelectedValue}" from the xaml, then it works.