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)
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)
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());
}
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...
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