ComboBox SelectedValue or SelectedItem Binding WPF C #

I have a combobox that is bound to ObservableCollectionstrings in an object. This binding works, but I also want to bind everything that the user selects from this combobox, in another property, which is a string, in the same object. I cannot figure out whether to use SelectedValueeither SelectedItem, or if there is a problem that goes beyond that. Thanks in advance.

Here is what I have so far, I have omitted any code that is not related to the problem:

In XAML:

<Grid.Resources>
    <my:JobItem x:Key="jobItemViewSource" />
</Grid.Resources>

<ComboBox x:Name="businessUnitBox" ItemsSource="{Binding Path=BusinessUnits}" IsSynchronizedWithCurrentItem="True">
    <ComboBox.SelectedValue>
        <Binding Path="BusinessUnit" Mode="TwoWay" UpdateSourceTrigger="Explicit" />
    </ComboBox.SelectedValue>
</ComboBox>

Code behind:

public string BusinessUnit
{
    get{  return businessUnit; }
    set
    {
        if (String.IsNullOrEmpty(BusinessUnit) || !BusinessUnit.Equals(value))
        {
            businessUnit = value;
            OnPropertyChanged("BusinessUnit");
        }
    }

}

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

        businessUnits = value;
    }
}
+5
source share
1 answer

, SelectedItem. , . SelectedValue SelectedValuePath... . , , , , UpdateSourceTrigger Explicit. .

+8

All Articles