Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a problem with getting twoway binding to work in WPF. I have
C#
public List<AirlineMaintenanceMVVM> Maintenances { get; set; }


where AirlineMaintenanceMVVM is:
C#
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:

XML
 <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
Posted
Updated 11-Oct-14 23:27pm
v3
Comments
Imran_Hasan 12-Oct-14 13:59pm    
Make sure you called the constructor (AirlineMaintenanceMVVM) with "selected" object which has to be an object from your "centers" collection.

Irrelevant: You can directly assign "centers" collection parameter into your Centers property in constructor, No need of "ForEach".
pjank42 12-Oct-14 14:51pm    
Thanks for the inputs and I am sure that the selected object is a part of the collection, so that is not the issue

1 solution

Hey pjank42,

just remove SelectedValuePath="Name" from the ComboBox, that's all :-)

You're trying to bind the Name Property (probably of type string) into the SelectedType variable which is a MaintenanceCenterMVVM.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900