Hi
I have a problem with getting twoway binding to work in WPF. I have
public List<AirlineMaintenanceMVVM> Maintenances { get; set; }
where AirlineMaintenanceMVVM is:
public class AirlineMaintenanceMVVM
{
private MaintenanceCenterMVVM _selectedtype;
public AirlinerMaintenanceType Type { get; set; }
public MaintenanceCenterMVVM SelectedType
{
get
{
return this._selectedtype;
}
set
{
this._selectedtype = value;
this.NotifyPropertyChanged("SelectedType");
}
}
public ObservableCollection<MaintenanceCenterMVVM> Centers { get; set; }
public AirlineMaintenanceMVVM(AirlinerMaintenanceType type, MaintenanceCenterMVVM selected, List<MaintenanceCenterMVVM> centers)
{
this.Type = type;
this.Centers = new ObservableCollection<MaintenanceCenterMVVM>();
centers.ForEach(c => this.Centers.Add(c));
this.SelectedType = selected;
}
#region Public Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Methods
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
In the xaml I have created a ListBox with a ComboBox inside, where the combo box binds to the elements and selected value from the MVVM-file. But eventhough I set the selected value when I create the MVVM-object, the value is not set when the combo box is shown:
<ListBox ItemsSource="{Binding Maintenances}" removed="Transparent" BorderThickness="0" ItemContainerStyleSelector="{StaticResource ListBoxItemStyleSelector}" VerticalAlignment="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Type.Name}" VerticalAlignment="Bottom" FontWeight="Bold" Width="100">
</TextBlock>
<ComboBox ItemsSource="{Binding Centers,Mode=OneTime}" Width="250"
Style="{StaticResource ComboBoxTransparentStyle}"
SelectedItem="{Binding SelectedType,Mode=TwoWay}"
DisplayMemberPath="Name" SelectedValuePath="Name" />
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
additional information copied from non-solution below
The selected value files in the mvvm object is correctly updated when I change the combobox