0

Hi I am trying to bind my ComboBox selected Item to a property in my view model where a setter will take this value and perform some other logic. Now my ComboBox is working correctly pulling items off an observablecollection Systems however I have been unable to bind the selectedItem which is a serial to a property. The selected item is not getting the string value of the ComboBox. Eveything else is ok the DataContext is assigned to the view in the code behind. Any ideas this is my viewModel:

public class CablingRequests : ObservableCollection<CablingRequest>
{
    public ObservableCollection<CablingRequest> PendingRequests { get; set; }
    public ObservableCollection<CablingRequest> ProcessedRequests { get; set; }
    public ObservableCollection<CablingRequest> Systems { get; set; }
    public ObservableCollection<CablingRequest> SelectedSystemConfiguration { get; set; }

    private string _serial;
    public string Serial
    {
        get { return _serial; }
        set
        {
            if (_serial == value)
                return;
            _serial = value;
            GetSelectedSystemConfiguration(_serial);
        }
    }

And my xaml code of the combobox:

<ComboBox x:Name="ComboBoxSerial" ItemsSource="{Binding Path=Systems}"
 DisplayMemberPath="SerialNumber" SelectedValue="{Binding Path=Serial, Mode=TwoWay}"
 IsSynchronizedWithCurrentItem="True" MinWidth="150" />
devdigital
  • 34,151
  • 9
  • 98
  • 120
user2350144
  • 33
  • 2
  • 5

1 Answers1

2

Your combobox is bound to a collection of CablingRequest, so you should either bind the SelectedItem to an instance of CablingRequest, or if you just want the serial number, then you should set the SelectedValuePath to the 'SerialNumber' property of the CablingRequest type.

See Difference between SelectedItem, SelectedValue and SelectedValuePath for more information.

Community
  • 1
  • 1
devdigital
  • 34,151
  • 9
  • 98
  • 120
  • The link was very helpful and helped me with issues getting an XML populated ComboBox with bindings to a string property to work as expected. – loganjones16 Mar 26 '19 at 22:57