Binding a list to an Observable collection from a type string in WPF

I am trying to create an application that tracks JobItems. In the application, the user clicks the Create Job button to create the JobItem. Then a new window opens (NewJobWindow), and the user must fill in the job information. Several required data have several meanings. For example, you can add several (string) business units within a given element. What I did was added a list for users to add all business units to it. What I don’t know how to do is link this list of business units so that every time I add an element to it, the Observable Collection BusinessUnits in JobItem gets the same element when I click the submit button. I need to know how I will do this using data binding. I already searched Google to search for similar answers, but could not find.

Edit

This is what I have in my JobItem class that I need to update every time a user submits multiple BusinessUnits to NewJobWindow:

public ObservableCollection<string> BusinessUnit
    {
        get { return businessUnit; }
        set
        {
            if(!BusinessUnit.Equals(value))
            {
                businessUnit = value;
                OnPropertyChanged("BusinessUnit");
            }
        }

    }

This is what JobWindow in xaml looks like to add business nodes to the list. I created a ValidatingListBox to verify that the user has inserted an item in 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>
+3
source share
1 answer

Binding is very simple:

<ListBox ItemsSource="{Binding MyCollection}" />  

where MyCollection is a property of type ObservableCollection. You do not need to add items to the ListBox, add them to the collection, data binding will do the rest.

+4
source

All Articles