JohnPaul
JohnPaul

Reputation: 77

Binding Combobox Name using Id value

I have a combobox control. I am getting combobox values from Database. There I have Id and Name. But I am binding only Name in the combobox. what i want is, 1. If I select any name in the Combobox, I need to save the Corresponding Id in my Database. Now I am able to bind the value from database and Display the Name in Combobox. but when I select any value in combobox and try to save means, it shows null value on the ID. Here is my code. Please help me to find some solution. Xaml:

    <ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0"    VerticalAlignment="Top" Height="35" Width="200"
                                          SelectedValue="{Binding MasterRentalType}"
                                          DisplayMemberPath="RentalTypeName"
                                          SelectedValuePath="RentalTypeId" />

My code Behind:

        var status = new MasterRentalType();
        List<MasterRentalType> listRentalType =
                              status.Get<MasterRentalType>() as
                               List<MasterRentalType>;

        cb_rentaltype.ItemsSource = listRentalType;

and i want to bind the Id to the Data context. here is the code,

 private void FacilityDataBind()
    {
        cb_rentaltype.DataContext = ??
    }

Note: MasterRentalType is the Table where i get the Values. There I have ID and Name values.

  public class MasterRentalType : EntityBase
     {
        public string RentalTypeId {get; set;}
        public string RentalTypeName {get; set;}       
     }

how can I bind and save the id value?

Upvotes: 0

Views: 7767

Answers (2)

Debasis
Debasis

Reputation: 458

There are several ways to achieve this. One of them is implemented below:

The xaml should look like this:

<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0" 
              ItemsSource="{Binding MasterRentalTypeColl, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
              SelectedItem="{Binding SelectedMasterRentalType, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
              DisplayMemberPath="RentalTypeName">            
    </ComboBox>

The MasterRentalType class:

public class MasterRentalType : EntityBase, INotifyPropertyChanged 
{
    private string _RentalTypeId;
    private string _RentalTypeName;

    public string RentalTypeId
    {
        get { return _RentalTypeId; }
        set
        {
            _RentalTypeId = value;
            NotifyPropertyChanged("RentalTypeId");
        }
    }

    public string RentalTypeName
    {
        get { return _RentalTypeName; }
        set
        {
            _RentalTypeName = value;
            NotifyPropertyChanged("RentalTypeName");
        }
    }



    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

The model class:

public class MasterRentalModel: INotifyPropertyChanged 
{
    private MasterRentalType _SelectedMasterRentalType;
    private List<MasterRentalType> _MasterRentalTypeColl = new List<MasterRentalType>();

    public MasterRentalType SelectedMasterRentalType
    {
        get { return _SelectedMasterRentalType; }
        set
        {
            _SelectedMasterRentalType = value;
            NotifyPropertyChanged("SelectedMasterRentalType");
        }
    }

    public List<MasterRentalType> MasterRentalTypeColl
    {
        get { return _MasterRentalTypeColl; }
        set { _MasterRentalTypeColl = value; }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

And in the code-behind, you just need to assign the model to the DataContext of you ComboBox; you can implement this any other function (here I have implemented it in the event handler of Loaded event of my Window):

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        MasterRentalModel masterRentalModel = new MasterRentalModel();

        // Fill the list of RentalType here
        masterRentalModel.MasterRentalTypeColl.Add(new MasterRentalType() { RentalTypeId = "1", RentalTypeName = "Monthly" });
        masterRentalModel.MasterRentalTypeColl.Add(new MasterRentalType() { RentalTypeId = "2", RentalTypeName = "Quarterly" });
        masterRentalModel.MasterRentalTypeColl.Add(new MasterRentalType() { RentalTypeId = "1", RentalTypeName = "Yearly" });

        cb_rentaltype.DataContext = masterRentalModel;
    }

Whenever user changes the selection, SelectedMasterRentalType will be updated. And in the SelectedMasterRentalType, you can get both RentalTypeId and RentalTypeName. If you follow proper binding your model will always be updated; that's the essence of WPF.

Hope this will help.

Upvotes: 2

Sheridan
Sheridan

Reputation: 69959

Your problem is caused because you have data bound the ComboBox.SelectedValue property to your MasterRentalType property, which I assume is of type MasterRentalType, but then you set the SelectedValuePath property to RentalTypeId. So you're saying *make the SelectedValue use the string RentalTypeId property, but then data binding a MasterRentalType to it.

There are a number of solutions. Correcting your example, you should try this:

<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0" 
    SelectedValue="{Binding MasterRentalType.RentalTypeId}"
    DisplayMemberPath="RentalTypeName"
    SelectedValuePath="RentalTypeId" />

Alternatively, you could have done this:

<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0" 
    SelectedItem="{Binding MasterRentalType}"
    DisplayMemberPath="RentalTypeName" />

To find out more about the differences, please take a look at the How to: Use SelectedValue, SelectedValuePath, and SelectedItem page on MSDN.

Upvotes: 4

Related Questions