Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is WPF ComboBox item not updating?
    text
    copied!<p>I am not understanding why in my following example the "Billing Model" combobox doesn't display the property BillingModel.BillingModelDescription when the textbox does. After selection of a client, i want the combobox to display the current billng model description, but it stays blank. The textbox bound to the same thing does show the description. I have a collection of possible models as the ItemsSource, which is working fine. How do i update the Billing Model combobox upon selection of a client?</p> <p>Here's the XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Label Content="Client"/&gt; &lt;ComboBox ItemsSource="{Binding AllClientData}" DisplayMemberPath="EmployerStr" SelectedItem="{Binding SelectedClient}" Width="300"/&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Label Content="Billing Model:"/&gt; &lt;ComboBox ItemsSource="{Binding AllBillingModels}" DisplayMemberPath="BillingModelDescription" SelectedItem="{Binding SelectedClient.BillingModel}" Width="300"/&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Label Content="Billing Model" /&gt; &lt;TextBox Text="{Binding SelectedClient.BillingModel.BillingModelDescription}" Width="200"/&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; </code></pre> <p></p> <p>And the code-behind (this is just an example, I am using MVVM etc in the full app, but this serves my puurpose to illustrate the issue):</p> <pre><code>public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { AllClientData = new ObservableCollection&lt;ClientRate&gt;(); AllBillingModels = new ObservableCollection&lt;BillingModelType&gt;(); ClientRate uno = new ClientRate(); uno.BillingModel = new BillingModelType(); uno.BillingModel.BillingModelID = 3; uno.BillingModel.BillingModelDescription = "Free"; uno.ID = 01; uno.EmployerName = "Employer1"; ClientRate dos = new ClientRate(); dos.BillingModel = new BillingModelType(); dos.BillingModel.BillingModelID = 2; dos.BillingModel.BillingModelDescription = "Variable"; dos.ID = 02; dos.EmployerName = "Employer2"; ClientRate tre = new ClientRate(); tre.BillingModel = new BillingModelType(); tre.BillingModel.BillingModelID = 1; tre.BillingModel.BillingModelDescription = "Flat"; tre.ID = 01; tre.EmployerName = "Employer3"; AllClientData.Add(uno); AllClientData.Add(dos); AllClientData.Add(tre); BillingModelType one = new BillingModelType(); one.BillingModelID = 1; one.BillingModelDescription = "Flat"; BillingModelType two = new BillingModelType(); two.BillingModelID = 2; two.BillingModelDescription = "Variable"; BillingModelType three = new BillingModelType(); three.BillingModelID = 3; three.BillingModelDescription = "Free"; AllBillingModels.Add(one); AllBillingModels.Add(two); AllBillingModels.Add(three); InitializeComponent(); this.DataContext = this; } private ObservableCollection&lt;ClientRate&gt; _allClientData; public ObservableCollection&lt;ClientRate&gt; AllClientData { get { return _allClientData; } set { if (_allClientData != value) { _allClientData = value; FirePropertyChanged("AllClientData"); } } } private ClientRate _selectedClient; /// &lt;summary&gt; /// Gets/Sets Global SelectedClient object /// &lt;/summary&gt; public ClientRate SelectedClient { get { return _selectedClient; } set { if (_selectedClient != value) { _selectedClient = value; FirePropertyChanged("SelectedClient"); } } } //private BillingModelType _selectedBillingModel; //public BillingModelType SelectedBillingModel //{ // get // { // return _selectedBillingModel; // } // set // { // if (_selectedBillingModel != value) // { // _selectedBillingModel = value; // FirePropertyChanged("SelectedBillingModel"); // } // } //} private ObservableCollection&lt;BillingModelType&gt; _allBillingModels; /// &lt;summary&gt; /// Holds all possible billing model types /// &lt;/summary&gt; public ObservableCollection&lt;BillingModelType&gt; AllBillingModels { get { return _allBillingModels; } set { if (_allBillingModels != value) { _allBillingModels = value; FirePropertyChanged("AllBillingModels"); } } } public event PropertyChangedEventHandler PropertyChanged; protected void FirePropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class BillingModelType { /// &lt;summary&gt; /// Billing Model ID /// &lt;/summary&gt; public int? BillingModelID { get; set; } /// &lt;summary&gt; /// Billing Model Description /// &lt;/summary&gt; public string BillingModelDescription { get; set; } } public class ClientRate : INotifyPropertyChanged { /// &lt;summary&gt; /// Employer name with Employer ID in parentheses /// &lt;/summary&gt; public string EmployerStr { get { return EmployerName + " (" + ID + ")"; } } /// &lt;summary&gt; /// Employer ID /// &lt;/summary&gt; public int? ID { get; set; } private string _EmployerName; /// &lt;summary&gt; /// Employer Official Name /// &lt;/summary&gt; public string EmployerName { get { return _EmployerName; } set { if (_EmployerName != value) { _EmployerName = value; FirePropertyChanged("EmployerName"); } } } private BillingModelType _billingModel; /// &lt;summary&gt; /// Rate Type ID and Description /// &lt;/summary&gt; public BillingModelType BillingModel { get { return _billingModel; } set { if (_billingModel != value) { _billingModel = value; FirePropertyChanged("BillingModel"); } } } public event PropertyChangedEventHandler PropertyChanged; protected void FirePropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload