0

I have a model class that I wish to bind a combo box to. My plan was to have an object with two propertied. 1) an ObservableCollection that contains the items I want to populate the combo box with. 2) A string property that stores the value of the selected item. I cannot seem to get this to work and open to suggestions. I am Trying to follow MVVM as best as possible. The behavior I observe is an empty combo box.

The class looks like this.

    public class WellListGroup : Notifier
{

    private ObservableCollection<string> _headers;

    public ObservableCollection<string> headers
    {
        get { return this._headers; }
        set { this._headers = value; OnPropertyChanged("headers"); }
    }

    private string _selected;

    public string selected
    {
        get { return this._selected;}
        set { this._selected = value; OnPropertyChanged("selected");}
    }

}

Notifier looks like:

public class Notifier : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}

}

And my viewmodel makes a call to a data access layer that creates the following object i wish to bind to.

public class MainViewModel :  Notifier
{
    public static getWells gw = new getWells();
    public static ObservableCollection<string> headers = gw.getHeaders();


    public WellListGroup wlg = new WellListGroup {headers = headers, selected = null};


}

Data Access Layer - getHeaders()

public ObservableCollection<string> getHeaders()
{
    ObservableCollection<string> vals = new ObservableCollection<string>();
    WVWellModel wvm = new WVWellModel();

    var properties = getProperties(wvm);

    foreach (var p in properties)
    {
        string name = p.Name;
        vals.Add(name);
    }


    return vals;

}

Then the view:

<ComboBox DockPanel.Dock="Top" ItemsSource = "{Binding Path = wlg.headers}" SelectedItem="{Binding Path = wlg.selected}"></ComboBox>

View Code Behind (Where the Data Context is set)

public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
            MainViewModel mvm = new MainViewModel();
            DataContext = mvm;
        }
    }

App.xaml.cs

public partial class App : Application
{
    private void OnStartup(object sender, StartupEventArgs e)
    {
        Views.MainView view = new Views.MainView();
        view.Show();
}

private void APP_DispatcherUnhandledException(object sender,DispatcherUnhandledExceptionEventArgs e)
{
    MessageBox.Show(e.Exception.Message);
    e.Handled = true;
}

}

I have tried several iterations of this but cant for the life of me get this to work. I am presented with an empty combo box.

LCaraway
  • 1,257
  • 3
  • 20
  • 48
  • Possible duplicate: [Binding WPF ComboBox to a Custom List](http://stackoverflow.com/questions/561166/binding-wpf-combobox-to-a-custom-list) – Jared Stroebele Jan 23 '17 at 20:08

1 Answers1

0

I am going to assume DataContext is set to MainViewModel on the view.

I think you well list group should call OnPropertyChanged

public class MainViewModel :  Notifier
{
    public static getWells gw = new getWells();
    public static ObservableCollection<string> headers = gw.getHeaders();

    private WellListGroup _wlg = new WellListGroup {headers = headers, selected = null};
    public WellListGroup wlg 
    {
        get { return _wlg; }
        set { _wlg = value; OnPropertyChanged("wlg"); }
}

The combo box binding should look like this:

<ComboBox 
    ItemsSource = "{Binding wlg.headers}" 
    SelectedItem = "{Binding wlg.selected Mode=TwoWay}"
/>

If neither of those work I would make sure the MainViewModel is being instantiated and assigned to DataContext in the Page constructor or a page loaded event.

Here is a code project Tutorial that may help break down the binding process Step by Step WPF Data Binding with Comboboxes

Jared Stroebele
  • 574
  • 6
  • 25