Hi,
I have a view of my data being fed by a viewmodel, but I'm having some issues with comboboxes. The xaml looks like this:
<ComboBox Grid.Row="4" Grid.Column="1" Margin="2,2,10,2"
ItemsSource="{Binding TrialParentValues, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
SelectedItem="{Binding TrialParentObj, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, Mode=TwoWay}"
DisplayMemberPath="Description"
SelectedValuePath="PK"
SelectedValue="{Binding TrialParentObj.PK}"
IsEditable="True"
IsReadOnly="True"
Background="{Binding Path=BackgroundBrush}"/>
and binds the source and values as required. Initially I tried simply binding the selectedvalue, but found that I have to use selecteditem to get this to update.
The issue I get is that I need to amend the itemssource collection depending on some rules. A static method HelperRepository.trialLookupRepository.LookupList
is used to get the values from the db and populate the observablecollection. However, if the record is committed (ie. read only), I only want to show the actual value of the field, and return a copy of the values but with only this element in the collection being available. This appears to work fine (I suspect that the same issue I'm about to outline applies here, but because the field doesn't change it isn't noticed).
public ObservableCollection<LookupModelBase> TrialParentValues
{
get
{
ObservableCollection<LookupModelBase> _limitedlist = new ObservableCollection<LookupModelBase>();
//read only - restrict list
if (_trial.Location == "committed")
{
foreach (LookupModelBase item in HelperRepository.trialLookupRepository.LookupList)
{
if (_trial.TrialParentFk == item.PK)
{
LookupModelBase _item = new LookupModelBase();
_item.PK = item.PK;
_item.Description = item.Description;
_limitedlist.Add(_item);
}
}
return _limitedlist;
}
else
//this needs to exclude the current trial
return HelperRepository.trialLookupRepository.LookupList;
}
}
However, for write enabled records I want the collection to exclude the value representing the current trial - the combobox represents a parent trial field and therefore cannot be its own parent. If I try to create a copy of the collection with this value removed (in the same manner as when creating the readonly collection) and I save the record, the OnPropertyChanged("")
event causes the combobox to clear. If I navigate away from the record then back again, the actual value it contains is returned and is correct.
I've fiddled around with this for a while - it's definitely something to do with amending the itemssource - any ideas?
Cheers