I am trying to create an application that tracks JobItems. In the application, the user clicks on a “New Job” button to create a JobItem. A new window (NewJobWindow) then pops up and the user must fill in information about the Job. A few of the information needed has multiple values. For instance, you can add multiple (string) Business Units within a job Item. What I’ve done is added a listbox for users to add all the business units within it. What I don’t know how to do is bind this listbox of business units so that every time I add an item in it, the Observable Collection BusinessUnits in JobItem gets added the same item when I click the submit button. I need to know how I would do this using databinding. I have already searched Google to look for similar answers but could not find any.
Edit:
This is what I have in my JobItem Class that I need to get updated every time a user is submitting multiple BusinessUnits in the NewJobWindow:
public ObservableCollection<string> BusinessUnit
{
get { return businessUnit; }
set
{
if(!BusinessUnit.Equals(value))
{
businessUnit = value;
OnPropertyChanged("BusinessUnit");
}
}
}
This is what the JobWindow looks like in xaml for adding businessunits within the listbox. I created a ValidatingListBox just so I can validate that the user has inserted an item within the list box.:
<Label Grid.Column="0" Grid.Row="5">Business Unit:</Label>
<my:ValidatingListBox Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="5" Grid.RowSpan="1" x:Name="businessUnitBox" SelectionMode="Multiple" SelectionChanged="ValidatingListBox_SelectionChanged" ItemsSource="{Binding Path=BusinessUnit}" >
<my:ValidatingListBox.ValidationListener>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}" Path="BusinessUnit" Mode="TwoWay">
<Binding.ValidationRules>
<my:ListBoxValidationRule ValidatesOnTargetUpdated="True" ></my:ListBoxValidationRule>
</Binding.ValidationRules>
</Binding>
</my:ValidatingListBox.ValidationListener>
</my:ValidatingListBox>
Binding is very simple:
where MyCollection is a property of ObservableCollection type. You don’t need to add items to ListBox, add them to collection, data binding will do the rest.