I have a combobox that is binding to an ObservableCollection
of strings in an object. This binding works, but I also want to bind whatever the user selects from this combobox, in a different property which is a string, in the same Object. I cannot figure out whether I should use SelectedValue
or SelectedItem
, or if there is a problem beyond that. Thank you in advance.
Here is what I have so far, I have omitted any code irrelevant 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;
}
}