Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all
I'm trying to find a good complete example using MVVM to enable me to display/edit ListBox items within a datagrid and cant seem to find any.

...at the moment I've coded up my app and am able to get row data and row LB items to display along with my LB (picklist - cellEditingTemplate) items using ObservableCollection<t>. I need to be able to remove LB items from my DataGridTemplateColumn.CellTemplate and have the removed item displayed back in my DataGridTemplateColumn.CellEditingTemplate.

Most examples are of ComboBoxes which do not quite meet my requirements.

I have spaghetti code at moment as below.

Thanks in advance.

PS. The below controls are part of another parent UControl containing other controls.

Model (I think)
C#
private ObservableCollection<string> _PotIncHazSelectedCollection;

public ObservableCollection<string> PotIncHazSelectedCollection
{
   get { return _PotIncHazSelectedCollection; }
   set
   {
      _PotIncHazSelectedCollection = value;
      if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs("PotIncHazSelectedCollection"));
   }
}

private ObservableCollection<string> _PotIncHazLookupCollection;

public ObservableCollection<string> PotIncHazLookupCollection
{
   get { return _PotIncHazLookupCollection; }
   set
   {
      _PotIncHazLookupCollection = value;
      if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs("PotIncHazLookupCollection"));
   }
}


Code Behind (non MVVM)
C#
// Selected Potential Incidents or Hazards Column
String potHazSelectedQuery = "select PotentialHazard \"PotentialHazardSelected\" from JobSafetyAnalysisStepPotentialHazard where RefJobSafetyAnalysisStepID = '" + refStepId + "'";

DataTable refPotHazSelectedSteps = db.GetDataTable(potHazSelectedQuery);                        

ObservableCollection<string> colRefPotHazSelectedSteps = new ObservableCollection<string>();

foreach (DataRow refPotHazSelectedStepsRow in refPotHazSelectedSteps.Rows)
{
   colRefPotHazSelectedSteps.Add(refPotHazSelectedStepsRow["PotentialHazardSelected"].ToString());
}

// Lookup Potential Incidents or Hazards Column
String potHazLookupQuery = "select l.Value \"PotentialHazardLookup\""
   + "from Lookup_Miscellaneous l "
   + "where not exists (select 1 from JobSafetyAnalysisStepPotentialHazard s where l.Value = s.PotentialHazard and s.RefJobSafetyAnalysisStepID = '" + refStepId + "') "
   + "and l.Category = 'JSAStepPotentialHazard' "
   + "and l.isActive = 1 "
   + "order by l.Value";

DataTable refPotHazLookupSteps = db.GetDataTable(potHazLookupQuery);

ObservableCollection<string> colRefPotHazLookupSteps = new ObservableCollection<string>();

foreach (DataRow refPotHazLookupStepsRow in refPotHazLookupSteps.Rows)
{
   colRefPotHazLookupSteps.Add(refPotHazLookupStepsRow["PotentialHazardLookup"].ToString());
}


Selection on Mouse Double Click...

C#
private void listBoxPotIncHazSelected_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
   try
   {
      var myListBoxItem = ((ListBox)sender).SelectedItem;

      foreach (var t in JobStepItems)
      {
         ObservableCollection<String> PotIncHazSelectedCol = t.PotIncHazSelectedCollection;
         ObservableCollection<String> PotIncHazLookupCol = t.PotIncHazLookupCollection;

         PotIncHazSelectedCol.Remove((string)myListBoxItem);
         PotIncHazLookupCol.Add((string)myListBoxItem);
      }
   }
   catch (Exception)
   {
   }
}

private void listBoxPotIncHazLookup_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
   try
   {
      var myListBoxItem = ((ListBox)sender).SelectedItem;

      foreach (var t in JobStepItems)
      {
         ObservableCollection<String> PotIncHazSelectedCol = t.PotIncHazSelectedCollection;
         ObservableCollection<String> PotIncHazLookupCol = t.PotIncHazLookupCollection;

         PotIncHazSelectedCol.Add((string)myListBoxItem);
         PotIncHazLookupCol.Remove((string)myListBoxItem);
      }
   }
   catch (Exception)
   {
   }
}


code blocks corrected and indexation reduced
Posted
Updated 5-Jan-14 12:46pm
v5
Comments
Wayne Gaylard 3-Jan-14 1:53am    
Can you show us the code ?
SDavisworth 5-Jan-14 18:28pm    
Hi Wayne... will update my question to include code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900