Reputation: 10604
I have a pretty simple form for adding items to a datagrid. Here is the problem child, a combobox that works the first time someone click the button to add the item, but subsequent clicks does not return any value for the selected value:
<ComboBox SelectedValuePath="Content" SelectedValue="{Binding Mode=TwoWay, NotifyOnValidationError=True, Path=NewResource.ResourceType, ValidatesOnExceptions=True}" Grid.Row="2" Grid.Column="1" Margin="0,10,0,0">
<ComboBoxItem Content="AV" />
<ComboBoxItem Content="Room Setup" /></ComboBox>
And here is the code in the view-model that adds the resource:
if (NewResource.Name != string.Empty)
{
ProposalResource _pr = new ProposalResource()
{
CreatedBy = App.UserID,
CreatedOn = DateTime.Now,
ModifiedBy = App.UserID,
ModifiedOn = DateTime.Now,
Name = NewResource.Name,
ProposalID = CurrentProposal.ProposalID,
Quantity = NewResource.Quantity,
ResourceType = NewResource.ResourceType
};
CurrentProposal.ProposalResources.Add(_pr);
ctx.SubmitChanges();
NewResource.Name = "";
NewResource.Quantity = null;
NewResource.ResourceType = null;
RaisePropertyChange("NewResource");
}
My problem is that this works for the first insert. The Resource Type is picked up from the combobox and all is well. But a subsequent click does not return any selected value. Is there any reason why subsequent requests would not pick up the two-way binding?
Upvotes: 0
Views: 709
Reputation: 7264
This is a known bug with SL built in ComboBox, that if the underlying ItemsSource is modified, the SelectedValue binding gets broken. (It also breaks if you set the SelectedValue to null after it was a non-null value).
A hand-crafted workaround will be needed for this, my preferred method is to write an Interactivity Behavior (see for example here how to write your own) with an ItemsSource and SelectedValue property, I bind (or modify) these properties, while I modify the ComboBox's Items and SelectedItem property for code, and change the behavior's selected value on the ComboBox's SelectionChanged event rather then binding it.
Upvotes: 1