WPF Combobox binding and SelectedValue with SelectedValuePath

Cass picture Cass · Mar 24, 2017 · Viewed 8.8k times · Source

I am really struggling with data binding and the MVVM Methodology, though I like the concept I am just struggling. I have created a WPF for that has multiple comboboxes and a button. The first combobox will list database instance names. the remaining comboboxes will be populated after the button is clicked. Since I am having issues with the first, database instances, combobox I will only show my code for that. When the application starts up the combobox is loaded and the first item is selected, as expected. The issue is when I select a new name my method that I expect to get called does not. Can someone help me to understand why my method public DBInstance SelectedDBInstance is not getting executed when I have this in my XAML, SelectedValue="{Binding SelectedDBInstance, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}?

Here is my XAML for the database instances combobox. One question I have here is the "value" fpr SelectedValuePath, if I change it to say "DBInstanceName" it does not work.

 <ComboBox x:Name="cbxRLFDBInstances" ItemsSource="{Binding DBInstances}" 
                  SelectedValue="{Binding SelectedDBInstance, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                  SelectedValuePath="value" DisplayMemberPath="DBInstanceName"/>

Here is my ViewModel Code:

namespace DatabaseTest.ViewModel
{

class RLFDatabaseTableViewModel : INotifyPropertyChanged
{
    Utilities dbtUtilities = new Utilities();


    public RelayCommand LoadDBInfoCommand
    {
        get;
        set;
    }


    public RLFDatabaseTableViewModel()
    {
        LoadDBInstances();

        LoadDBInfoCommand = new RelayCommand(LoadDBInfo);
    }


    public ObservableCollection<DBInstance> DBInstances
    {
        get;
        set;
    }


    public void LoadDBInstances()
    {
        ObservableCollection<DBInstance> dbInstances = new ObservableCollection<DBInstance>();
        DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

        dbInstances.Add(new DBInstance { DBInstanceName = "fal-conversion\\mun2012ci" });
        dbInstances.Add(new DBInstance { DBInstanceName = "fal-conversion\\mun2014ci" });

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                dbInstances.Add(new DBInstance { DBInstanceName = dr["Name"].ToString() });
            }
        }

        DBInstances = dbInstances;

    }


    private DBInstance _selectedDBInstance;


    public DBInstance SelectedDBInstance
    {
        get
        {
            return _selectedDBInstance;
        }

        set
        {
            _selectedDBInstance = value;
            RaisePropertyChanged("SelectedDBInstance");
            //ClearComboBoxes();

        }
    }
}
}

Here is my Model code. When I step through the code this method, public string DBInstanceName, gets executed multiple time. I do not know why and it is seems wasteful to me.

namespace DatabaseTest.Model
{
public class RLFDatabaseTableModel { }


public class DBInstance : INotifyPropertyChanged
{
    private string strDBInstance;


    public override string ToString()
    {
        return strDBInstance;
    }


    public string DBInstanceName
    {
        get
        {
            return strDBInstance;
        }

        set
        {
            if (strDBInstance != value)
            {
                strDBInstance = value;
                RaisePropertyChanged("DBInstanceName");
            }
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

Answer

mm8 picture mm8 · Mar 26, 2017

You should bind the SelectedItem property of the ComboBox to the SelectedDBInstance property and get rid of the SelectedValuePath:

<ComboBox x:Name="cbxRLFDBInstances" ItemsSource="{Binding DBInstances}" 
              SelectedItem="{Binding SelectedDBInstance, UpdateSourceTrigger=PropertyChanged}" 
              DisplayMemberPath="DBInstanceName"/>

The SelectedValuePath property is only used when you want to bind to a source property that is not of the same type as the item in the ItemsSource collection.

To select an item initially you should set the SelectedDBInstance property to an item that is present in the DBInstances collection:

public RLFDatabaseTableViewModel()
{
    LoadDBInstances();
    LoadDBInfoCommand = new RelayCommand(LoadDBInfo);
    SelectedDBInstance = DBInstances[0]; //selected the first item
}